应用程序在调用 NSAttributedString.boundingRectWithSize 时崩溃

App crashed when invoking NSAttributedString.boundingRectWithSize

我想要一个 UILabel,其中部分文本带有下划线,类似于 link。这就是我构造 NSAttributedString:

的方式
let attributedStr = NSMutableAttributedString(string: "Some description.")
let underlineText = NSAttributedString(string: "Click Here", attributes: [
                NSAttributedString.Key.foregroundColor : UIColor.blue,
                NSAttributedString.Key.underlineStyle: NSUnderlineStyle.single,
                NSAttributedString.Key.font: UIFont.systemFont(ofSize: 14.0)])
attributedStr.append(underlineText)

let size = attributedStr.boundingRect(with: CGSize(width:CGFloat(width), height:CGFloat.greatestFiniteMagnitude), options: .usesLineFragmentOrigin, context: nil).size

应用程序在调用 boundingRectWith 方法时崩溃:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', 
reason: '-[__SwiftValue _getValue:forType:]: unrecognized selector sent to instance 0x28050bed0'

为什么?我应该如何正确测量文字的大小?

键的值 - NSAttributedString.Key.underlineStyle 应该在末尾包含原始值,就像这样 - NSUnderlineStyle.single.rawValue.

您可以看到下面的代码:

let attributedStr = NSMutableAttributedString(string: "Some description.")

let underlineText = NSAttributedString(string: "Click Here", attributes: [
                    NSAttributedString.Key.foregroundColor : UIColor.blue,
                    NSAttributedString.Key.underlineStyle: NSUnderlineStyle.single.rawValue,
                    NSAttributedString.Key.font: UIFont.systemFont(ofSize: 14.0)])

attributedStr.append(underlineText)