2

尝试显示 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))

所以我有我的字体标签,没关系,问题是我无法更改标签的链接颜色,这是我想要的系统蓝色。

知道如何更改链接的颜色吗?

谢谢。

4

4 回答 4

3

我在 swift 4.0 中找到了答案

termsAndPolicyTextView.linkTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.red]

完整代码:注意:我不能在单个 textView 中设置多种颜色。

    let attributedString = NSMutableAttributedString(string: termsAndPolicyText)

    attributedString.addAttribute(NSAttributedString.Key.link,
                                  value: "https://google.co.in",
                                  range: (termsAndPolicyText as NSString).range(of: "Terms or service")
    )

    attributedString.addAttribute(NSAttributedString.Key.link,
                                  value: "https://google.co.in", // Todo set our terms and policy link here
        range: (termsAndPolicyText as NSString).range(of: "Privacy & Legal Policy")
    )

    attributedString.addAttributes([NSAttributedString.Key.foregroundColor: UIColor.NMSTextColor(with: 0.6)],
                                   range: NSRange(location: 0, length: termsAndPolicyText.count))

    termsAndPolicyTextView.linkTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.termstextViewTextColor()]
    termsAndPolicyTextView.attributedText = attributedString
    termsAndPolicyTextView.textAlignment = .center

}
于 2018-12-04T10:47:50.067 回答
1

可以通过以下方式更改链接颜色。下面的例子将证明:

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。它具有linkAttributesactiveLinkAttributes属性,您可以使用它来实现所需的行为,而无需使用UITextView.

请让我知道它是否有效。随时提出修改建议以使其更好:)

于 2016-11-30T17:23:15.237 回答
1

好的,我找到了一个丑陋的方法来做到这一点。

将 html 文本转换为 NSMutableAttributedString 后,我只是循环考虑所有属性,当我看到“NSLink”属性时,我只需为属性的范围添加一个属性:

self.myString!.enumerateAttributes(in: NSRange(0..<myString!.length), options: []) { (attributes, range, _) -> Void in
                for (attribute, object) in attributes {
                    if attribute == "NSLink" {
                        print("Attribute = \(attribute) -- \(object)")
                        self.myString?.addAttribute(NSForegroundColorAttributeName, value: StyleKit.color_blue_bright, range: range)
                        self.myString?.addAttribute(NSUnderlineColorAttributeName, value: UIColor.clear, range: range)
                    }
                }
            }
于 2016-12-01T09:19:24.737 回答
0

链接颜色是属性字符串的默认值。可以使用 css 指定。

extension String {
    var html2AttributedString: NSAttributedString? {
        let html = """

<style type="text/css">
a, a:link, a:visited {
    color: inherit !important;
}
</style>
""" + self
        guard let data = html.data(using: .utf8) else { return nil }
        ...
于 2020-01-31T12:10:56.573 回答