gpt4 book ai didi

ios - 调用 NSAttributedString.boundingRectWithSize 时应用崩溃

转载 作者:行者123 更新时间:2023-11-28 13:23:02 25 4
gpt4 key购买 nike

我想要一个 UILabel,其中部分文本带有下划线以类似于链接。这就是我构建 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 应该在末尾包含 rawValue,就像这样 - 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)

关于ios - 调用 NSAttributedString.boundingRectWithSize 时应用崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58815727/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com