Displaying NSMutableAttributedString on iOS 8

后端 未结 3 1950
忘掉有多难
忘掉有多难 2021-01-01 21:39

Seems on iOS 8.0 (12A365) NSMutableAttributedString sometimes will not be displayed correctly. The problem obviously occurs when the range of the attribute does

相关标签:
3条回答
  • 2021-01-01 22:13

    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];
                                             }];
    
    0 讨论(0)
  • 2021-01-01 22:14

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

    0 讨论(0)
  • 2021-01-01 22:30

    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
    
    0 讨论(0)
提交回复
热议问题