uitableview - 在 iOS 8 上显示 NSMutableAttributedString

标签 uitableview uilabel ios8 xcode6 nsattributedstring

似乎在 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/

相关文章:

uitableview - rectForRowAtIndexPath 不适用于 iOS8

ios - UISearchBar 在搜索没有结果时显示标签,但在单击 UISearchBar 中的 x 时不会再次隐藏标签

iphone - UILabel 与 UILineBreakModeWordWrap 的结果行

ios - 这个 Today Extension (iOS8) 可能吗?

ios - 我在哪里放置 iOS 8 + Swift 中的常用实用程序函数

ios - 将数据从 plist 加载到两个 TableView

ios - 将 UIView 添加到重用的 UITableViewCell?

iphone - 在tableviewcontroller的编辑模式下重命名 "Delete"按钮

ios - UIStepper 没有更新相应的标签

iOS::如何使用 GZIP Utility 解压缩 .gz 文件?