I recently had a problem with a yellow label on a dynamic background that can sometimes also be yellow. The two common solutions are
- Add a shadow.
- Outline your text.
NSAttributedString offers everything you need to do it.
Filled & Outlined
1 2 3 4 5 6 7 8 9 10 |
var outlinedLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 300, height: 50)) let strokeTextAttributes = [ NSStrokeColorAttributeName : UIColor.blackColor(), NSForegroundColorAttributeName : UIColor.lightGrayColor(), NSStrokeWidthAttributeName : -4.0, NSFontAttributeName : UIFont.boldSystemFontOfSize(52) ] outlinedLabel.attributedText = NSAttributedString(string: "Outlined", attributes: strokeTextAttributes) |
Filled ?
Note that the stroke width attribute is a negative value.
1 |
NSStrokeWidthAttributeName : -4.0 |
This is to tell the drawing engine it is a “Stroke & Fill” operation. Otherwise it’ll just ignore the “NSForegroundColorAttributeName” and leave the text transparent like this :
Link to the playground : MABDev.OutlinedText.playground
Documentation : https://developer.apple.com/library/mac/qa/qa1531/_index.html
Cheers,
MAB
Your site has excellent web content. I bookmarked the site
Thank you very much ! I shall add more !