Cocoa Touch is absolutely amazing if you need to complete a sophisticated task like getting the GPS location of the device anywhere in the world, make an HD video and send it over to your web server, or get the orientation of the user towards the north pole.
However for quite some time now it’s been relatively complicated to do some simple text formatting – like having a bold word in the middle of a sentence, for example when using a UILabel. Also something that I often wished for was to have a button, which mimics a hyperlink, like the one on the image below:
Until iOS6 that was actually only possible by using the CoreText framework, which is indeed a very powerful text layout framework, but is not so straight forward to use – provided that it’s not tightly integrated with UIKit and also that is a C based framework (vs. being an Objective-C framework).
However, these days that I’m talking about are over now!
So, since I had the pleasure to write the chapter about NSAttributedString in iOS6 and cover all the new UIKit APIs that make use of the new attributed string functionality, I decided to include few samples here for your guys.
How to set the color of an attributed text
NSForegroundColorAttributeName is style attributed you use to set the foreground color of the text. Pretty easy, right?
How to set the background color of an attributed text
NSBackgroundColorAttributeName is the style attribute you use to set the background of the text.
How to set the font of an attributed text
Finally you can use the NSFontAttributeName to set the font name and the size of the font.
As you can see using attributed strings in iOS6 is tons of fun and if you want to read in detail about all the power you can add to your apps with NSAttributedString in iOS6, do have a look at “iOS6 by Tutorials” – now at a lower introductory price.
Cheers, Marin
The post was originally published on the following URL: http://www.touch-code-magazine.com/using-attributed-strings-in-ios6/