Displaying NSMutableAttributedString on iOS 8
Asked Answered
L

3

14

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.

Latchstring answered 19/9, 2014 at 11:13 Comment(3)
I've been seeing this as well with trying to underline a portion of a label. This is a really annoying regression.Mishandle
@Darcy: Thanks for your comment. You could try to add another dummy attribute (like clear colored background ...) at (NSRange){0,1} - for me this is a workaround for 1.) as mentioned above.Latchstring
Did you submit a bug report to Apple? I'm running in the exact same issue here.Bulkhead
C
9

Try this, first apply an extra NSBackgroundColorAttributeName in whole of the label with a transparent color

text = [[NSMutableAttributedString alloc] initWithString:@"Green is green. (-> Bug)"];
[text addAttribute:NSBackgroundColorAttributeName value:[UIColor clearColor] range:(NSRange){0,text.length}]; //Fix
[text addAttribute:NSBackgroundColorAttributeName value:[UIColor greenColor] range:(NSRange){9,5}];
cell.label.attributedText=text
Catch answered 5/12, 2014 at 13:10 Comment(2)
This is a fix and should be at least considered to be the correct answerBulkhead
Had the same problem in iOS8 where text strikethrough not showing up at middle ranges. This fixed it - 10x. bug should be reported to apple.Organizer
A
0

I'm running into similar issues with NSAttributedString and the NSLinkAttributeName attribute in iOS 8. Specifically, a that starts at the beginning of the string (e.g. "www.cnn.com") displays as expected (using drawWithRect:options:context:). However, if we add the link beyond the first character (e.g "text www.cnn.com") the link does not show as blue or underlined, despite the attributes being correct. These strings usually do have attributes which encompass the entire string and that doesn't seem to matter, perhaps because the "whole string" attributes are added after the NSLink attributes. I've tried swapping the order so the links are added after other attributes, but that doesn't fix the issue.

The only workaround that I have figured out so far is to re-attribute the links with an underline, after adding the attributes that apply to the entire string, like this:

        [_attributedDisplayValue enumerateAttribute:NSLinkAttributeName
                                            inRange:displayValueRange
                                            options:0
                                         usingBlock:^(id value, NSRange range, BOOL *stop) {
                                             [_attributedDisplayValue addAttribute:NSUnderlineColorAttributeName
                                                                             value:[UIColor blueColor]
                                                                             range:range];
                                         }];
Allowed answered 22/9, 2014 at 22:4 Comment(1)
Thank you for your answer! Your workaround seems for me similar or same as described in my comment to Darcy. Bad news by the way, in iOS 8.0.2 the issue is still there ...Latchstring
E
0

It's no longer an issue (iOS 9.0) - screenshot

Elman answered 20/10, 2015 at 18:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.