【发布时间】:2014-09-19 11:13:28
【问题描述】:
似乎在 iOS 8.0 (12A365) NSMutableAttributedString 上有时无法正确显示。当属性的范围不是从文本的开头开始(并且如果没有其他属性从文本的开头开始)时,问题显然会出现。
因此使用 1.) 第二个单词“green”将不会显示绿色背景(错误!)(“cell”是 UITableViewCell,UILabel“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