在iOS 8上显示NSMutableAttributedString [英] Displaying NSMutableAttributedString on iOS 8
问题描述
似乎在iOS 8.0(12A365) NSMutableAttributedString
有时无法正确显示。当属性的范围没有从文本的开头开始时(如果没有其他属性从文本的开头开始),问题显然会发生。
Seems on iOS 8.0 (12A365) NSMutableAttributedString
sometimes will not be displayed correctly. The problem obviously occurs when the range of the attribute does not start at the beginning of the text (and if there is no other attribute starting at the beginning of the text).
因此1.)第二个单词green将不显示绿色背景(bug!)(cell是一个 UITableViewCell
带有 UILabel
标签作为子视图):
So with 1.) the second word "green" in will not show the green background (bug!) ("cell" is a UITableViewCell
with UILabel
"label" as a 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.)背景显示正确:
With 2.) and 3.) the backgrounds are displayed correctly:
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项目:截图和XCode 6项目
Find screenshot and XCode 6 Project here: Screenshot and XCode 6 Project
对我来说似乎是iOS 8中的一个错误 - 所以报告发给Apple。
Seems for me as a bug in iOS 8 - so a report goes to Apple.
推荐答案
试试这个,先申请一个额外的整个标签中的NSBackgroundColorAttributeName具有透明色
Try this, first apply an extra NSBackgroundColorAttributeName in whole of the label with a transparent color
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
这篇关于在iOS 8上显示NSMutableAttributedString的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!