Search code examples
iosswiftnsattributedstringsetattributensmutableattributedstring

How to remove attributes from the NSAttributedString swift?


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?


Solution

  • Use the removeAttribute method:

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