我使用以下代码来浏览文本
NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:(text ? text : @"")];
[attributeString addAttribute:NSStrikethroughStyleAttributeName
value:@2
range:NSMakeRange(0, [attributeString length])];
label.attributedText = attributeString;
文字为白色,因此删除线也是白色。我想把它改成红色。我怎么能这样做?
答案 0 :(得分:15)
根据NSAttributedString UIKit Additions Reference,您可以设置
NSStrikethroughColorAttributeName
属于您选择的UIColor
。
答案 1 :(得分:11)
添加" NSStrikethroughColorAttributeName"属性为字符串
[attributedString addAttributes:@{NSStrikethroughStyleAttributeName: @(NSUnderlineStyleSingle)
, NSStrikethroughColorAttributeName: [UIColor redColor]
, NSBackgroundColorAttributeName: [UIColor yellowColor]}
文档为here
答案 2 :(得分:7)
SWIFT 3.x
要删除带有颜色的文本标签,您需要2个属性,一个用于NSStrikethroughStyleAttributeName,一个用于NSStrikethroughColorAttributeName。我发现它更具可读性。
let attributedString = NSMutableAttributedString(string: "0,91 €")
attributedString.addAttribute(NSStrikethroughStyleAttributeName, value: NSNumber(value: NSUnderlineStyle.styleSingle.rawValue), range: NSMakeRange(0, attributedString.length))
attributedString.addAttribute(NSStrikethroughColorAttributeName, value: UIColor.red, range: NSMakeRange(0, attributedString.length))
答案 3 :(得分:4)
在Swift中:
attributeString.addAttribute(NSStrikethroughColorAttributeName, value: UIColor.redColor(), range: NSMakeRange(0, attributeString.length))
答案 4 :(得分:3)
SWIFT 4.2
@llker Baltaci的Swift 4.2答案更新
let attributedString = NSMutableAttributedString(string: "0,91 €")
attributedString.addAttribute(NSAttributedString.Key.strikethroughStyle, value: NSNumber(value: NSUnderlineStyle.single.rawValue), range: NSMakeRange(0, attributedString.length))
attributedString.addAttribute(NSAttributedString.Key.strikethroughColor, value: UIColor.darkGray, range: NSMakeRange(0, attributedString.length))