首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >更改NSAttributedString html链接颜色

更改NSAttributedString html链接颜色
EN

Stack Overflow用户
提问于 2016-12-01 00:21:25
回答 4查看 4.5K关注 0票数 2

尝试显示ASTextNode (与AsyncDisplayKit中的UILabel相同)以显示html文本。我只需设置标签的属性文本。

下面是我的字符串的工作方式:

使用这个扩展,我将HTML文本转换为NSAttributedString:

代码语言:javascript
运行
复制
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 ?? ""
    }
}

然后我设置我的标签详细信息:

代码语言:javascript
运行
复制
 self.displayContent = NSMutableAttributedString(attributedString: content.html2AttributedString!)
 self.displayContent?.addAttribute(NSFontAttributeName, value: UIFont.fontMainFeedContentFont(), range: NSRange.init(location: 0, length: self.displayContent!.length))

所以我有我的标签和我的字体,它是好的,问题是我不能改变我标签的链接颜色,它是我想要的系统蓝色。

你知道怎么改变链接的颜色吗?

谢谢。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2018-12-04 18:47:50

我在swift 4.0中找到了这个问题的答案

代码语言:javascript
运行
复制
termsAndPolicyTextView.linkTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.red]

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

代码语言:javascript
运行
复制
    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

}
票数 1
EN

Stack Overflow用户

发布于 2016-12-01 01:23:15

可以通过以下方式更改链接颜色。下面的示例将演示这一点:

代码语言:javascript
运行
复制
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进行以下更改。

代码语言:javascript
运行
复制
textView.linkTextAttributes = [NSForegroundColorAttributeName : UIColor.black]

如果你不想使用UITextView来做这件事,你可以简单地使用TTTAttributedLabel。它具有linkAttributesactiveLinkAttributes属性,您可以使用这些属性在不使用UITextView的情况下实现所需的行为。

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

票数 1
EN

Stack Overflow用户

发布于 2016-12-01 17:19:25

好了,伙计们,我发现了一个丑陋的方法。

在将html文本转换为NSMutableAttributedString后,我简单地循环考虑了所有属性,当我看到"NSLink“属性时,我只需为该属性的范围添加一个属性:

代码语言:javascript
运行
复制
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)
                    }
                }
            }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40893223

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档