尝试显示 ASTextNode(与 AsyncDisplayKit 中的 UILabel 相同)以显示 html 文本。我只需要设置标签的属性文本。
我如何处理我的字符串:
使用此扩展,我将 HTML 文本转换为 NSAttributedString :
extension String {
var html2AttributedString: NSAttributedString? {
guard let data = data(using: .utf8) else { return nil }
do {
return try NSAttributedString(data: data, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: String.Encoding.utf8.rawValue], documentAttributes: nil)
} catch let error as NSError {
print(error.localizedDescription)
return nil
}
}
var html2String: String {
return html2AttributedString?.string ?? ""
}
}
然后我设置我的标签详细信息:
self.displayContent = NSMutableAttributedString(attributedString: content.html2AttributedString!)
self.displayContent?.addAttribute(NSFontAttributeName, value: UIFont.fontMainFeedContentFont(), range: NSRange.init(location: 0, length: self.displayContent!.length))
所以我有我的字体标签,没关系,问题是我无法更改标签的链接颜色,这是我想要的系统蓝色。
知道如何更改链接的颜色吗?
谢谢。