Search code examples
swiftuilabelios14nsmutableattributedstringstrikethrough

Why strikethrough is not working in iOS 14


I have one UILabel which shows strikethrough text (e.g. text). It was working fine till iOS 13. In iOS 14, that strikethrough line is not showing, i.e. instead of 'text', it is coming as 'text'. This is the code I used:

let totalString: String = "some text"
let strikeString: String = "text" //string to be made strikethrough
let totalNsString = totalString as NSString
let attributeString =  NSMutableAttributedString(string: totalString)
attributeString.addAttribute(NSAttributedString.Key.strikethroughStyle, value: 2, range: totalNsString.range(of: strikeString))
label.attributedText = attributeString

Desired Output:

some text

Current Outout:

some text

Can anyone please tell me is there anything I need to change or anything else. TIA.


Solution

  • Adding baselineOffset with value 0 to the attributed string brings back the strikethrough line. More details can be found on apple dev forum and this SO thread.

    attributeString.addAttribute(NSAttributedString.Key.baselineOffset, value: 0, range: totalNsString.range(of: strikeString))