ios - 如何禁用 NSMutableAttributedString 中标签的下划线 url

标签 ios swift nsmutableattributedstring

我有一个带有属性文本的标签。文本包含 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 中标签的下划线 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55245733/

相关文章:

ios - 将 Flutter Downloader 插件集成到 AppDelegate.swift 中

ios - 视口(viewport)背景图像小偏移

ios - 没有单元格重用的 UITableView 中的问题

c - os x swift 构建包管理器无法从 unixODBC C 依赖项中找到共享库

swift - NSTextField 不可见

ios - NSMutableAttributedString 中的 Tab 是什么

ios - 如果我在设备的 Safari 上付款,应用程序会被拒绝吗?

swift - 如何检索请求的 Alamofire 响应 header

xcode - 在 NSScrollView 中显示文本 (Swift)

objective-c - HTML 到 NSMutableAttributedString 在 ios 6 中不起作用