2
votes

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.

4
By link color, do you mean underline color? - KrishnaCA
Nope, the text color is black, the links colors are blue - user2206906

4 Answers

1
votes

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

}
1
votes

The link color can be changed in the following way. The below example will demonstrate that:

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

Also, you have to make the following changes to the UITextView that's displaying it.

textView.linkTextAttributes = [NSForegroundColorAttributeName : UIColor.black]

If you do not want to use UITextView to do this, you can simply use TTTAttributedLabel. It has linkAttributes and activeLinkAttributes property which you can use to achieve the desired behaviour without using UITextView.

Please let me know if it works or not. Feel free to suggest edits to make this better :)

1
votes

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)
                    }
                }
            }
0
votes

Links color is default of attributed string. Can be specified by using 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 }
        ...