How to remove attributes from the NSAttributedString swift?

Salome Tsiramua picture Salome Tsiramua · Nov 19, 2015 · Viewed 7.2k times · Source

I have added some attributes to my buttons 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)

How can I remove NSStrikethroughStyleAttributeName from it after button click?

Answer

Eric Aya picture Eric Aya · Nov 19, 2015

Use the removeAttribute method:

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