小编Bra*_*oni的帖子

如何检测从 html 字符串创建的 NSAttributedString 中的锚标记链接点击

我正在使用 html 创建 NSAttributedString:

let htmlString = "<body style='padding-left:50px'><h1>Hello World</h1><div><a href=https://apple.com/offer/samsung-faq/>Click Here</a></div><p>This is a sample text</p><pre>This is also sample pre text</pre></body>"
Run Code Online (Sandbox Code Playgroud)

在这里我使用扩展方法将其设置为 UILabel

someLabel.attributedText = htmlString.htmlToAttributedString
Run Code Online (Sandbox Code Playgroud)

NSAttributedString 扩展:

extension String {
    var htmlToAttributedString: NSAttributedString? {
        guard let data = data(using: .utf8) else { return NSAttributedString() }
        do {
            return try NSAttributedString(data: data, options: [NSAttributedString.DocumentReadingOptionKey.documentType:  NSAttributedString.DocumentType.html], documentAttributes: nil)
        } catch {
            return NSAttributedString()
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

在这里,我想要一个回调方法来检测 html 字符串中作为锚标记存在的链接。我将如何获得点击事件以及如何获得该事件回调中的网址?

请帮忙...

nsattributedstring ios swift

3
推荐指数
1
解决办法
2054
查看次数

标签 统计

ios ×1

nsattributedstring ×1

swift ×1