Hi this is Marin - the author of Touch Code Magazine, I hope you are enjoying my tutorials and articles. Also if you need a bright iPhone developer overseas contact me - I do contract work. Here's my LinkedIn profile
Using attributed strings in iOS6

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

    UIColor* fgColor = [UIColor greenColor];
    NSDictionary* style = @{
        NSForegroundColorAttributeName: fgColor,
    };
 
    NSAttributedString* myString = [[NSAttributedString alloc] initWithString:@"Touch Code Magazine"
                                                                   attributes:style];

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

    UIColor* bgColor = [UIColor orangeColor];
    NSDictionary* style = @{
        NSBackgroundColorAttributeName: bgColor
    };
 
    NSAttributedString* myString = [[NSAttributedString alloc] initWithString:@"Touch Code Magazine"
                                                                   attributes:style];

NSBackgroundColorAttributeName is the style attribute you use to set the background of the text.

How to set the font of an attributed text

    UIFont* myFont = [UIFont fontWithName:@"Zapfino" size:18.0];
    NSDictionary* style = @{
        NSFontAttributeName: myFont
    };
 
    NSAttributedString* myString = [[NSAttributedString alloc] initWithString:@"Touch Code Magazine"
                                                                   attributes:style];

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/

 

  ·

 


Marin Todorov

is an independent iOS developer and publisher. He's got more than 18 years of experience in a dozen of languages and platforms. This is his writing project.
» Contact    » Add Marin on Google+

  1. Amy Plant on Wednesday 19, 2012

    You might want to file a copyright infringement:
    http://ios.biomsoft.com/2012/11/13/using-attributed-strings-in-ios6/