13

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?

Salome Tsiramua
  • 763
  • 3
  • 9
  • 21

3 Answers3

11

Use the removeAttribute method:

attr.removeAttribute(NSStrikethroughStyleAttributeName, range: NSMakeRange(0, attr.length))
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
  • i want to remove the strike data, when i click on a button, but by this code always i am getting unstriked button text – SCS Apr 06 '19 at 05:01
4

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))
ipraba
  • 16,485
  • 4
  • 59
  • 58
1

In Swift 5

let attributeString: NSMutableAttributedString =  NSMutableAttributedString(string: "YourStringHere")
attributeString.removeAttribute(NSAttributedString.Key.strikethroughStyle, range: NSMakeRange(0, attributeString.length))
yourLblHere.attributedText = attributeString
Naresh
  • 16,698
  • 6
  • 112
  • 113