gpt4 book ai didi

ios - 如何在标签的 NSMutableAttributedString 中禁用带下划线的网址

转载 作者:行者123 更新时间:2023-11-28 05:44:44 29 4
gpt4 key购买 nike

我有一个带有属性文本的标签。文本具有 url 链接,该链接带有默认蓝色下划线。如何去除 NSMutableAttributedString 的 url 下划线样式?

func htmlToAttributedString(_ html: String) -> NSAttributedString? {
guard let data = NSString(string: html).data(using: String.Encoding.utf8.rawValue) else { return nil }
do {
let attrStr = try NSAttributedString(data: data,
options: [.documentType: NSAttributedString.DocumentType.html, .characterEncoding: String.Encoding.utf8.rawValue],
documentAttributes: nil)
let range = NSRange(location: 0, length: attrStr.length)
let str = NSMutableAttributedString(attributedString: attrStr)
str.addAttributes([NSAttributedString.Key.font: UIFont.systemFont(ofSize: 17.0)], range: range)
str.addAttribute(NSAttributedString.Key.underlineStyle, value: 0, range: range)
return NSAttributedString(attributedString: str.attributedSubstring(from: range))
} catch {}
return nil
}

我试过上面的代码,但它仍然显示装饰链接。

attributed-string-url

最佳答案

枚举 attributedString 中的属性,并删除链接的那些...

attributedString.enumerateAttributes(in: NSRange(location: 0, length: attributedString.length), options: []) { attributes, range, stop in                
attributedString.removeAttribute(.link, range: range)
attributedString.removeAttribute(.foregroundColor, range: range)
attributedString.removeAttribute(.underlineStyle, range: range)

}

关于ios - 如何在标签的 NSMutableAttributedString 中禁用带下划线的网址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55245733/

29 4 0