Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change NSAttributedString html links color

Trying to display a ASTextNode (same as UILabel from AsyncDisplayKit) to display an html text. I simply have to set the label's attributed text.

There is how i work my string :

Using this extension i transform the HTML text into a 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 ?? ""
    }
}

Then i set my label details :

 self.displayContent = NSMutableAttributedString(attributedString: content.html2AttributedString!)
 self.displayContent?.addAttribute(NSFontAttributeName, value: UIFont.fontMainFeedContentFont(), range: NSRange.init(location: 0, length: self.displayContent!.length))

So i have my label with my font and it's ok, problem is that i can't change the links colors of my label, it's a system blue that i do want.

Any idea how can i change the links' colors ?

Thanks.

like image 200
user2206906 Avatar asked Mar 18 '26 13:03

user2206906


2 Answers

I found an answer for this in Swift 4.0

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

Full code: Note: I can't set multiple color in a single 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
like image 133
Sarath Avatar answered Mar 20 '26 05:03

Sarath


Ok guys, i found an ugly way to do this.

After transforming the html text to an NSMutableAttributedString, i simply loop thought all attributes, when i see an "NSLink" attribute i simply add an attribute for the attribute's range :

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)
                    }
                }
            }
like image 22
user2206906 Avatar answered Mar 20 '26 05:03

user2206906



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!