Seems on iOS 8.0 (12A365) NSMutableAttributedString
sometimes will not be displayed correctly. The problem obviously occurs when the range of the attribute does not start at the beginning of the text (and if there is no other attribute starting at the beginning of the text).
So with 1.) the second word "green" in will not show the green background (bug!) ("cell" is a UITableViewCell
with UILabel
"label" as a subview):
1.)
text = [[NSMutableAttributedString alloc] initWithString:@"Green is green. (-> Bug)"];
[text addAttribute:NSBackgroundColorAttributeName value:[UIColor greenColor] range:(NSRange){9,5}];
cell.label.attributedText=text
With 2.) and 3.) the backgrounds are displayed correctly:
2.)
text = [[NSMutableAttributedString alloc] initWithString:@"Green is green. (-> Ok)"];
[text addAttribute:NSBackgroundColorAttributeName value:[UIColor greenColor] range:(NSRange){0,5}];
[text addAttribute:NSBackgroundColorAttributeName value:[UIColor greenColor] range:(NSRange){9,5}];
cell.label.attributedText=text
3.)
text = [[NSMutableAttributedString alloc] initWithString:@"Green is green. (-> Ok)"];
[text addAttribute:NSBackgroundColorAttributeName value:[UIColor greenColor] range:(NSRange){0,5}];
cell.label.attributedText=text
Find screenshot and XCode 6 Project here: Screenshot and XCode 6 Project
Seems for me as a bug in iOS 8 - so a report goes to Apple.