尝试显示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))所以我有我的标签和我的字体,它是好的,问题是我不能改变我标签的链接颜色,它是我想要的系统蓝色。
你知道怎么改变链接的颜色吗?
谢谢。
发布于 2018-12-04 18:47:50
我在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
}发布于 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的情况下实现所需的行为。
请让我知道它是否有效。请随时建议修改以使其更好:)
发布于 2016-12-01 17:19:25
好了,伙计们,我发现了一个丑陋的方法。
在将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)
}
}
}https://stackoverflow.com/questions/40893223
复制相似问题