How to remove attributes from NSAttributedString swift?

I added some attributes to my attribute buttons

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) 

How can I remove NSStrikethroughStyleAttributeName from it after clicking a button?

+12
ios swift nsattributedstring
source share
3 answers

Use the removeAttribute method:

 attr.removeAttribute(NSStrikethroughStyleAttributeName, range: NSMakeRange(0, attr.length)) 
+9
source share

It is very simple. You can use this method from NSMutableAttributedString class

 func removeAttribute(_ name: String, range range: NSRange) 

In your case

 attr.removeAttribute(NSStrikethroughStyleAttributeName , range:NSMakeRange(0, attr.length)) 
+4
source share

In Swift 5

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

All Articles