我正在使用 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 字符串中作为锚标记存在的链接。我将如何获得点击事件以及如何获得该事件回调中的网址?
请帮忙...