Saturday, October 19, 2013

Introduction to NSAttributedString and NSMutableAttributedString Class


This is new in iOS 6. It is really powerful and really useful. Think of NSAttributedString as an NString where each character has dictionary of attributes. The attributes attached to the character can be its font, the color, is it underline etc etc. Thats how you can think of NSAttributedString as conceptually.

However its not like that It is very efficient and it handles the attributes very efficiently. You will know as we progress through the article.


How to we get the attributes from the NSAttributed string. here is the syntax.

-(NSDictionary *)attributesAtIndex : (NSUInteger) index effectiveRange : (NSRangePointer) range;

it returns how many characters have exactly the same dictionary of Attributes.

It is Not an NSString

Another thing you might need to think is that NSAttributedString is not NSString, it does not inherit from NSString either. You cannot send string messages to NSAttributedString. But still of you want to perform some string operations like replacing a char etc. than you will call this method string on NSAttributedString like shown below.

NSAttributedString *attributedString = ...;
NSRange r = [[attributedString string] rangeOfString:subString];


Unline NSString we always use NSMutableAttributedString, because we want to consistently modify the attributes in the string. Methods which will let you modify the attributes of the attributed strings are:

- (void) addAttributes:(NSDictionary *) attributes range:(NSRange *)range;
This method will allow you to add new attributes and do not touch the other attributes.

- (void) removeAttributes:(NSDictionary *) attributes range:(NSRange *)range;
Remove the attributes for a given range. If you remove an attribute than the default of  that attribute will replace at. Every attribute have its default.

- (void) setAttributes:(NSDictionary *) attributes range:(NSRange *)range;
It will set the newly passed attributes and will clear the existing attributes. 

Make sure while adding attributes to the string you choose wisely between setAttributes and addAttributes methods. Usually you would like to use addAttributes because if you are adding an attribute which already exist than the newly added attribute will win.


Also isEqual behave differently for attributed string. It look for the exact match of the string characters and also the attributes addd to the string characters. so if your strings have multiple attributes added it is unlikely that isEqual will find a match. 

Changing characters of mutable attributed string

Now you want to change the actual characters of the NSMutableAttributedString. There exists a method called mutableString which will help to modify the string and the attributes remains. :)

Defining Attributes

In iOS, standard attribute keys are described in the “Constants” section of NSAttributedString UIKit Additions Reference. Here are few examples of defining attributes.

Now I want my text to be of font size 12 and blue color. how to i defined the attributes for this? here it is
@{ NSFontAttributeName : [UIFont systemFontOfSize : 12], NSForegroundColorAttributeName : [UIColor blueColor] }

How Do I set label String to be Attributed?

Drag a label on to the main storyboard and in the utilities window change the text mode from Plain to Attributed. As shown in image below.

It is introduction to the attributed string and will come up with examples in next blog..

Happy Coding....

No comments:

Post a Comment