【问题标题】:Change NSAttributedString html links color更改 NSAttributedString html 链接颜色
【发布时间】: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


【解决方案1】:

我在 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

}

【讨论】:

    【解决方案2】:

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

    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

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

    【讨论】:

    • 这不是我使用的textview,我不能在tableview中,我不想计算高度,我会试试谢谢你
    • NSUnderlineStyleAttributeName 只是在链接下划线,我想更改链接的文本颜色。
    • 我相信你已经尝试过使用attributedText.addAttribute(NSForegroundColorAttributeName, value: UIColor.black, range: NSMakeRange(0, attributedText.length))。它对我不起作用。只有UITextViewlinkTextAttributes 属性,这是不使用coretext 的唯一方法
    • 您可以在 tableView 中使用UITextView 并使其不可编辑。否则,其中一种方法是使用TTTAttributedLabelgithub.com/TTTAttributedLabel/TTTAttributedLabel
    • 不,我需要实现 60 fps 的滚动...没有 UITextView 和大小计算,所以看起来不可能...谢谢。
    【解决方案3】:

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

    将 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)
                        }
                    }
                }
    

    【讨论】:

      【解决方案4】:

      链接颜色是属性字符串的默认值。可以通过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 }
              ...
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-12-26
        • 1970-01-01
        • 1970-01-01
        • 2012-07-29
        • 1970-01-01
        • 2017-07-01
        相关资源
        最近更新 更多