似乎在 iOS 8.0 (12A365) 上 NSMutableAttributedString
有时不会正确显示。当属性的范围不是从文本开头开始时(并且如果没有其他属性从文本开头开始),问题显然会发生。
因此,对于 1.) 中的第二个单词“green”将不会显示绿色背景(错误!)(“单元格”是 UITableViewCell
,带有 UILabel
“标签”作为 subview ):
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 提交了一份报告。
最佳答案
试试这个,首先在整个标签中应用一个额外的 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
关于uitableview - 在 iOS 8 上显示 NSMutableAttributedString,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25932632/