Using attributed strings in iOS6

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/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值