尝试显示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))所以我有我的标签和我的字体,它是好的,问题是我不能改变我标签的链接颜色,它是我想要的系统蓝色。
你知道怎么改变链接的颜色吗?
谢谢。
发布于 2016-12-01 01:23:15
可以通过以下方式更改链接颜色。下面的示例将演示这一点:
let attributedText:NSMutableAttributedString = NSMutableAttributedString(string: "why?")
attributedText.addAttribute(NSUnderlineStyleAttributeName, value: NSUnderlineStyle.styleSingle, range: NSMakeRange(0, attributedText.length))
attributedText.addAttribute(NSUnderlineColorAttributeName, value: UIColor.black, range: NSMakeRange(0, attributedText.length))
attributedText.addAttribute(NSForegroundColorAttributeName, value: UIColor.black, range: NSMakeRange(0, attributedText.length))此外,您还必须对显示它的UITextView进行以下更改。
textView.linkTextAttributes = [NSForegroundColorAttributeName : UIColor.black]如果你不想使用UITextView来做这件事,你可以简单地使用TTTAttributedLabel。它具有linkAttributes和activeLinkAttributes属性,您可以使用这些属性在不使用UITextView的情况下实现所需的行为。
请让我知道它是否有效。请随时建议修改以使其更好:)
https://stackoverflow.com/questions/40893223
复制相似问题