Adding strikethrough to NSAttributedString in iOS 11 with Swift

eskimo picture eskimo · Mar 22, 2018 · Viewed 7.6k times · Source

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.

Answer

Fitsyu picture Fitsyu · Jul 27, 2018

I use this to strike through a text in Xcode9/iOS11/Swift4 env

  let strokeEffect: [NSAttributedStringKey : Any] = [
        NSAttributedStringKey.strikethroughStyle: NSUnderlineStyle.styleSingle.rawValue,
        NSAttributedStringKey.strikethroughColor: UIColor.red,
    ]

   let strokeString = NSAttributedString(string: "text", attributes: strokeEffect)