为什么下划线样式不适用于 NSAttributedString?

Cha*_*ish 6 uikit nsattributedstring uilabel ios swift

我有以下代码来尝试为标签的字符串添加下划线。

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
Run Code Online (Sandbox Code Playgroud)

但是,运行代码时,标签显示Hello World!没有下划线。

我正在运行 Xcode 12.4,并使用 iOS 14.4 iPhone 12 Pro Max 模拟器。

Cha*_*ish 11

看来这是一个错误。将值从 更改为NSUnderlineStyle.single可以1解决该问题。

最终代码如下所示。

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