我正在我的 tableview 单元格中阅读这个 json 响应。我已将其设置为 UILabel 并设法将其显示为我的标签的属性刺痛。我的观点是将标签内的文本显示为链接,但点击链接不起作用。我想要的是点击这些链接打开浏览器。
{"Disclaimer":"I have read & accept the <a href=\"https:\/\/staging.rentalcover.com\/pds\/W80P-J8ZP-INS\" target=\"_blank\">Policy Terms<\/a>. I have read & accept the <a href=\"https:\/\/staging.rentalcover.com\/pds\/W80P-J8ZP-INS\">Policy Terms<\/a>. This policy is issued by RentalCover.com, AFSL No.490058 and underwritten by Assetinsure Pty Ltd."}
下面是我的代码
var htmlResponse:String = obj.Disclaimer ?? ""
let myText = htmlResponse
lbl_Disclaimer.attributedText = self.stringFromHtml(string: myText)
lbl_Disclaimer.font = UIFont(name: "Poppins-Regular", size: 13)
// MARK:- HTML TO STRING
func stringFromHtml(string: String) -> NSAttributedString? {
do {
let data = string.data(using: String.Encoding.utf8, allowLossyConversion: true)
if let d = data {
let str = try NSAttributedString(data: d,
options: [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html],
documentAttributes: nil)
return str
}
} catch {
}
return nil
}
我在 json 响应中获取文本“政策条款”的链接,并希望它可以点击以在浏览器中打开链接。
谢谢