【问题标题】:Displaying NSMutableAttributedString on iOS 8在 iOS 8 上显示 NSMutableAttributedString
【发布时间】:2014-09-19 11:13:28
【问题描述】:

似乎在 iOS 8.0 (12A365) NSMutableAttributedString 上有时无法正确显示。当属性的范围不是从文本的开头开始(并且如果没有其他属性从文本的开头开始)时,问题显然会出现。

因此使用 1.) 第二个单词“green”将不会显示绿色背景(错误!)(“cell”是 UITableViewCellUILabel“label”作为子视图):

1.)

text = [[NSMutableAttributedString alloc] initWithString:@"Green is green. (-> Bug)"];
[text addAttribute:NSBackgroundColorAttributeName value:[UIColor greenColor] range:(NSRange){9,5}];
cell.label.attributedText=text

使用 2.) 和 3.) 正确显示背景:

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

在此处查找屏幕截图和 XCode 6 项目:Screenshot and XCode 6 Project

在我看来是 iOS 8 中的一个错误 - 因此向 Apple 报告。

【问题讨论】:

  • 我在尝试为标签的一部分下划线时也看到了这一点。这是一个非常烦人的回归。
  • @Darcy:感谢您的评论。您可以尝试在 (NSRange){0,1} 处添加另一个虚拟属性(例如清晰的彩色背景...) - 对我来说,这是 1.) 的解决方法,如上所述。
  • 您是否向 Apple 提交了错误报告?我在这里遇到完全相同的问题。

标签: uitableview uilabel ios8 xcode6 nsattributedstring


【解决方案1】:

试试这个,首先在整个标签中应用一个额外的 NSBackgroundColorAttributeName,颜色为透明

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

【讨论】:

  • 这是一个修复,至少应该被认为是正确的答案
  • 在 iOS8 中遇到了同样的问题,文本删除线没有出现在中间范围内。这修复了它 - 10x。错误应该报告给苹果。
【解决方案2】:

我在 iOS 8 中遇到了与 NSAttributedString 和 NSLinkAttributeName 属性类似的问题。具体来说,从字符串开头开始的 a(例如“www.cnn.com”)按预期显示(使用 drawWithRect:options:语境:)。但是,如果我们在第一个字符之外添加链接(例如“text www.cnn.com”),则链接不会显示为蓝色或带下划线,尽管属性正确。这些字符串通常确实具有包含整个字符串的属性,这似乎并不重要,可能是因为“整个字符串”属性是在 NSLink 属性之后添加的。我尝试交换顺序,以便在其他属性之后添加链接,但这并不能解决问题。

到目前为止,我想出的唯一解决方法是在添加适用于整个字符串的属性之后,用下划线重新赋予链接属性,如下所示:

        [_attributedDisplayValue enumerateAttribute:NSLinkAttributeName
                                            inRange:displayValueRange
                                            options:0
                                         usingBlock:^(id value, NSRange range, BOOL *stop) {
                                             [_attributedDisplayValue addAttribute:NSUnderlineColorAttributeName
                                                                             value:[UIColor blueColor]
                                                                             range:range];
                                         }];

【讨论】:

  • 感谢您的回答!对我来说,您的解决方法似乎与我对达西的评论中描述的相似或相同。顺便说一下坏消息,在 iOS 8.0.2 中问题仍然存在......
【解决方案3】:

这不再是问题 (iOS 9.0) - screenshot

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多