如何从 NSAttributedString swift 中删除属性?

How to remove attributes from the NSAttributedString swift?

我已将一些属性添加到我的按钮 attributedTitle

 let attr = NSMutableAttributedString(string: currTitle)

 attr.addAttribute(NSStrikethroughStyleAttributeName, value: 2, range: NSMakeRange(0, attr.length))
 attr.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor(), range: NSMakeRange(0,  attr.length))

 currButton?.setAttributedTitle(attr, forState: UIControlState.Normal)

单击按钮后如何从中删除 NSStrikethroughStyleAttributeName?

很简单。您可以使用 NSMutableAttributedString class

中的此方法
func removeAttribute(_ name: String,
               range range: NSRange)

你的情况

attr.removeAttribute(NSStrikethroughStyleAttributeName , range:NSMakeRange(0, attr.length))

使用removeAttribute方法:

attr.removeAttribute(NSStrikethroughStyleAttributeName, range: NSMakeRange(0, attr.length))

在Swift5

let attributeString: NSMutableAttributedString =  NSMutableAttributedString(string: "YourStringHere")
attributeString.removeAttribute(NSAttributedString.Key.strikethroughStyle, range: NSMakeRange(0, attributeString.length))
yourLblHere.attributedText = attributeString