Why is underline style not working for a NSAttributedString?

Viewed 771

I have the following code to try to underline a label's string.

let attributedString = NSMutableAttributedString(string: "Hello World!")
attributedString.addAttribute(NSAttributedString.Key.underlineStyle, value: NSUnderlineStyle.single, range: NSRange.init(location: 0, length: attributedString.length))
label.attributedText = attributedString

However, when running the code the label shows Hello World! with no underline.

I'm running Xcode 12.4, and using an iOS 14.4 iPhone 12 Pro Max simulator.

1 Answers

It seems like this is a bug. Changing the value from NSUnderlineStyle.single to 1 fixes the issue.

The final code would look something like the following.

let attributedString = NSMutableAttributedString(string: "Hello World!")
attributedString.addAttribute(NSAttributedString.Key.underlineStyle, value: 1, range: NSRange.init(location: 0, length: attributedString.length))
label.attributedText = attributedString
Related