【发布时间】:2016-11-30 16:21:24
【问题描述】:
尝试显示一个 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))
所以我的标签带有我的字体,没关系,问题是我无法更改标签的链接颜色,这是我想要的系统蓝色。
知道如何更改链接的颜色吗?
谢谢。
【问题讨论】:
-
链接颜色是指下划线颜色吗?
-
不,文本颜色为黑色,链接颜色为蓝色
标签: ios nsattributedstring asyncdisplaykit