Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 424
Salome Tsiramua Avatar asked Nov 19 '15 14:11

Salome Tsiramua


Video Answer


2 Answers

Use the removeAttribute method:

attr.removeAttribute(NSStrikethroughStyleAttributeName, range: NSMakeRange(0, attr.length))
like image 100
Eric Aya Avatar answered Oct 03 '22 18:10

Eric Aya


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))
like image 43
ipraba Avatar answered Oct 03 '22 20:10

ipraba