如何为标签添加删除线样式。UILabel 中数字的中间删除行。检查附加图像以供参考。
问问题
6217 次
5 回答
3
使用属性字符串
截屏
代码
NSDictionary * attribtues = @{NSStrikethroughStyleAttributeName:@(NSUnderlineStyleSingle),
NSStrikethroughColorAttributeName:[UIColor redColor]};
NSAttributedString * attr = [[NSAttributedString alloc] initWithString:@"label" attributes:attribtues];
self.label.attributedText = attr;
于 2015-11-20T03:40:10.177 回答
2
来自@krunal Swift4 答案的更新函数
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()
于 2020-01-14T15:58:33.947 回答
1
如果想使用 UILabel 扩展
extension UILabel {
func strikeThroughText() {
let attributeString = NSMutableAttributedString(string: self.text ?? "")
attributeString.addAttribute(NSAttributedString.Key.strikethroughStyle, value: NSUnderlineStyle.single.rawValue, range: NSMakeRange(0,attributeString.length))
self.attributedText = attributeString
}
}
于 2020-12-29T20:06:03.620 回答
1
以编程方式使用以下代码
更喜欢使用扩展:
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()
结果看起来像这样。
于 2018-12-28T12:55:11.070 回答