Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding strikethrough to NSAttributedString in iOS 11 with Swift

Having some issues getting strikethrough to work. Currently I'm doing the following:

theString.addAttributes([
        NSAttributedStringKey.strikethroughStyle: NSUnderlineStyle.styleSingle.rawValue, 
        NSAttributedStringKey.strikethroughColor: UIColor.white
    ], range: NSMakeRange(0, 1))

It's not showing any sort of strikethrough though. Any ideas? I can't seem to find anything that works.

like image 507
eskimo Avatar asked Mar 21 '18 23:03

eskimo


1 Answers

Swift 5.1

let attributedText : NSMutableAttributedString =  NSMutableAttributedString(string: "Your Text")
attributedText.addAttributes([
                NSAttributedString.Key.strikethroughStyle: NSUnderlineStyle.single.rawValue,
                NSAttributedString.Key.strikethroughColor: UIColor.lightGray,
                NSAttributedString.Key.font : UIFont.systemFont(ofSize: 12.0)
                ], range: NSMakeRange(0, attributedText.length))
like image 50
Amrit Tiwari Avatar answered Sep 19 '22 21:09

Amrit Tiwari