如何UILabel
在Swift中强调一下?我搜索了Objective-C但却无法让它们在Swift中工作.
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.
我有一个不可变的属性字符串,没有NSStrikethroughStyleAttributeName属性,如下所示:
NSMutableAttributedString *str1 = [[NSMutableAttributedString alloc] initWithString:@"aaaa" attributes:@{NSStrikethroughStyleAttributeName:[NSNumber numberWithInteger:NSUnderlineStyleSingle]}];
Run Code Online (Sandbox Code Playgroud)
和另一个带有NSStrikethroughStyleAttributeName属性的可变属性字符串,如下所示:
NSMutableAttributedString *str2 = [[NSMutableAttributedString alloc] initWithString:@"bbbb"];
Run Code Online (Sandbox Code Playgroud)
以及包含上面两个字符串的整个字符串:
NSMutableAttributedString *labelString = [[NSMutableAttributedString alloc] init];
[labelString appendAttributedString:str1];
[labelString appendAttributedString:str2];
Run Code Online (Sandbox Code Playgroud)
当我将整个字符串附加到UILabel时:
_label.attributedText = labelString;
Run Code Online (Sandbox Code Playgroud)
它在iOS7和iOS8中都表现良好,如下所示:
AAAABBBB
但是当我交换他们的职位时:
[labelString appendAttributedString:str2];
[labelString appendAttributedString:str1];
Run Code Online (Sandbox Code Playgroud)
它在iOS7中正确显示,但在iOS8中没有正确显示
ios7:bbbbAAAA ios8:bbbbaaaa
似乎在iOS8中,如果属性字符串中的第一个字符不是strikethroughed,则UILabel不会正确渲染删除线.我认为这是iOS8中的一个错误,是否有人遇到过与我相同的问题?
nsattributedstring strikethrough ios nsmutableattributedstring ios8
我试图强调一下UIBarButtonItem
,但以下内容没有效果:
testBarItem.setTitleTextAttributes([
NSUnderlineStyleAttributeName:NSUnderlineStyle.StyleDouble.rawValue
], forState: .Normal)
Run Code Online (Sandbox Code Playgroud)
改变其他属性,比如NSForegroundColorAttributeName
工作.
上面使用的相同代码与a NSAttributedString
和a UILabel
强调文本,所以为什么这不适用于UIBarButtonItem
?