如何为标签添加strikethroughStyle。在UILabel中删除一个数字的中间一行。检查附加图像以供参考。
发布于 2015-11-20 03:40:10
使用attributeString
屏幕截图
代码
NSDictionary * attribtues = @{NSStrikethroughStyleAttributeName:@(NSUnderlineStyleSingle),
NSStrikethroughColorAttributeName:[UIColor redColor]};
NSAttributedString * attr = [[NSAttributedString alloc] initWithString:@"label" attributes:attribtues];
self.label.attributedText = attr;
发布于 2020-01-14 15:58:33
Swift4 @krunal应答中更新的函数
extension String {
func strikeThrough() -> NSAttributedString {
let attributeString = NSMutableAttributedString(string: self)
attributeString.addAttribute(NSAttributedString.Key.strikethroughStyle, value: NSUnderlineStyle.single.rawValue, range: NSMakeRange(0,attributeString.length))
return attributeString
}
}
调用这个函数是相同的
yourLabel.attributedText = "yourString".strikeThrough()
发布于 2018-12-28 12:55:11
用于以下代码的编程使用
更喜欢使用扩展:
extension String {
func strikeThrough() -> NSAttributedString {
let attributeString = NSMutableAttributedString(string: self)
attributeString.addAttribute(NSAttributedStringKey.strikethroughStyle, value: NSUnderlineStyle.styleSingle.rawValue, range: NSMakeRange(0,attributeString.length))
return attributeString
}
}
并调用此函数
myLabel.attributedText = "my string".strikeThrough()
结果是这样的。
https://stackoverflow.com/questions/33818529
复制相似问题