NSMutableAttributedString is a great asset to IOS development that allows developer to customize UILable text at certain positions. In this we will learn, how to use NSMutableAttributedString in IOS. Let us say, i want my string to be displayed as
You are looking at attributed string.
To achieve this, i have two choices
1) Create two separate UILable’s
2) Use NSMutableAttributedString and assign that to my UILable
Second option is far better then first one. In this post, we will use NSMutableAttributedString to achieve the above result/output and showcase different things that we can done by using NSMutableAttributedString.
Create NSMutableAttributedString instance,
NSMutableAttributedString * stringTiming = [[NSMutableAttributedString alloc] initWithString:@”You are looking at attributed string.”];
In above line of code, we created an instance of NSMutableAttributedString and init it with our constant string.
To modify the above string, we can use addAttribute method of NSMutableAttributedString.
[stringTiming addAttribute:NSFontAttributeName value:[UIFont fontWithName:@”Verdana-Bolditalic” size:20.0] range:NSMakeRange(0, 8)];
lable.attributedText= stringTiming;
In above line of code we give attribute name as and set its value to Verdana-Bolditalic font, since we required our first two words in same font with size 20.0f. Range parameter will take range of characters those are to be modified. NSMakeRange, will take two values, location and length. Above we want our string to be modified from start that’s why we set location to zero and length to eight as we counted those characters length in the above string. Finally, we set our attributed string to label as attributed text.
Note: lable is an instance of UILable declared in .h file as IBOutlet.
Complete code:
NSMutableAttributedString * stringTiming = [[NSMutableAttributedString alloc] initWithString:@”You are looking at attributed string.”];
[stringTiming addAttribute:NSFontAttributeName value:[UIFont fontWithName:@”Verdana-Bolditalic” size:20.0] range:NSMakeRange(0, 8)];
lable.attributedText= stringTiming;
Output: |
Output of above code |
Underline Attribute:
Complete code:
NSMutableAttributedString * stringTiming = [[NSMutableAttributedString alloc] initWithString:@”You are looking at attributed string.”];
[stringTiming addAttribute:NSFontAttributeName value:[UIFont fontWithName:@”Verdana-Bolditalic” size:20.0] range:NSMakeRange(0, 8)];
[stringTiming addAttribute:NSUnderlineStyleAttributeName value:@”1″ range:NSMakeRange(0, 8)];
lable.attributedText= stringTiming;
Output:
|
Output Underline |
Strike through a line Attribute:
Complete code:
NSMutableAttributedString * stringTiming = [[NSMutableAttributedString alloc] initWithString:@”You are looking at attributed string.”];
[stringTiming addAttribute:NSStrikethroughStyleAttributeName value:@”1″ range:NSMakeRange(0,[stringTiming length])];
lable.attributedText= stringTiming;
Output:
|
Output strike through |
Foreground Color Attribute:
Complete code:
NSMutableAttributedString * stringTiming = [[NSMutableAttributedString alloc] initWithString:@”You are looking at attributed string.”];
[stringTiming addAttribute:NSFontAttributeName value:[UIFont fontWithName:@”Verdana-Bolditalic” size:20.0] range:NSMakeRange(0, 8)];
[stringTiming addAttribute:NSUnderlineStyleAttributeName value:@”1″ range:NSMakeRange(0, 8)];
[stringTiming addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 8)];
lable.attributedText= stringTiming;
Output:
|
Output foreground color |
Background Color Attribute:
Complete code:
NSMutableAttributedString * stringTiming = [[NSMutableAttributedString alloc] initWithString:@”You are looking at attributed string.”];
[stringTiming addAttribute:NSFontAttributeName value:[UIFont fontWithName:@”Verdana-Bolditalic” size:20.0] range:NSMakeRange(0, 8)];
[stringTiming addAttribute:NSUnderlineStyleAttributeName value:@”1″ range:NSMakeRange(0, 8)];
[stringTiming addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 8)];
[stringTiming addAttribute:NSBackgroundColorAttributeName value:[UIColor yellowColor] range:NSMakeRange(0,[stringTiming length])];
lable.attributedText= stringTiming;
Output:
|
Output Background Color |
Writing direction Attribute: Complete code:
NSMutableAttributedString * stringTiming = [[NSMutableAttributedString alloc] initWithString:@”You are looking at attributed string.”];
[stringTiming addAttribute:NSWritingDirectionAttributeName value:@[@(NSWritingDirectionRightToLeft | NSTextWritingDirectionOverride)] range:NSMakeRange(0, [stringTiming length])];
lable.attributedText= stringTiming;
Output:
|
Output writing direction right to left |