14 uitableview nsattributedstring uilabel xcode6 ios8
iOS 8.0(12A365)上的似乎NSMutableAttributedString有时无法正确显示.当属性的范围不在文本的开头(并且如果在文本的开头没有其他属性开始)时,显然会出现问题.
所以1.)第二个单词"green"将不显示绿色背景(bug!)("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
Run Code Online (Sandbox Code Playgroud)
使用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
Run Code Online (Sandbox Code Playgroud)
3.)
text = [[NSMutableAttributedString alloc] initWithString:@"Green is green. (-> Ok)"];
[text addAttribute:NSBackgroundColorAttributeName value:[UIColor greenColor] range:(NSRange){0,5}];
cell.label.attributedText=text
Run Code Online (Sandbox Code Playgroud)
在此处查找屏幕截图和XCode 6项目:屏幕截图和XCode 6项目
对我来说似乎是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
Run Code Online (Sandbox Code Playgroud)
我在 iOS 8 中遇到了 NSAttributedString 和 NSLinkAttributeName 属性的类似问题。具体来说,从字符串开头(例如“www.cnn.com”)开始的 a 按预期显示(使用drawWithRect:options:context:) 。但是,如果我们将链接添加到第一个字符之外(例如“文本 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];
}];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5205 次 |
| 最近记录: |