Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iphone/ipad: How exactly use NSAttributedString?

Yes, many people are saying about Rich Text in iPhone/iPad and many knows about NSAttributedString.

But how to use NSAttributedString? I searched for much time, no extract clues for this.

I know how to set up a NSAttributedString, then what should I do to display a text on iPhone/iPad with rich text?

The official docs says it should be used with CoreText.Framework, what does that mean?

Is there any simple way like this?

NSAttributedString *str; ..... UILabel *label; label.attributedString = str; 
like image 653
Jack Avatar asked Sep 24 '10 11:09

Jack


1 Answers

Starting from the iOS 6.0 you can do it like that:

NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"Hello. That is a test attributed string."]; [str addAttribute:NSBackgroundColorAttributeName value:[UIColor yellowColor] range:NSMakeRange(3,5)]; [str addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(10,7)]; [str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"HelveticaNeue-Bold" size:20.0] range:NSMakeRange(20, 10)]; label.attributedText = str; 
like image 129
RomanN Avatar answered Sep 24 '22 10:09

RomanN