Luda
Luda

Reputation: 7078

Change links colors and underline color of html converted to AttributedString

I would like to color the links and give special color to the underline of the links in my text that received from html.

This is what I have at the moment:

...

public func htmlStyleAttributeText(text: String) -> NSMutableAttributedString? {

        if let htmlData = text.data(using: .utf8) {

            let options: [NSAttributedString.DocumentReadingOptionKey: Any] = [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html, NSAttributedString.DocumentReadingOptionKey.characterEncoding: String.Encoding.utf8.rawValue]

            let attributedString = try? NSMutableAttributedString(data: htmlData, options: options, documentAttributes: nil)

            let attributes: [NSAttributedString.Key: AnyObject] = [NSAttributedString.Key.foregroundColor: UIColor.red]
            attributedString?.addAttributes(attributes, range: NSRange.init(location: 0, length: attributedString?.length ?? 0))
            return attributedString
        }

        return nil
    }
....

This is what I get: enter image description here

What I am looking for is regular color for the text and red for the links and green for the underline of the links

Upvotes: 3

Views: 2829

Answers (2)

Mischa
Mischa

Reputation: 17269

The color for the text is red because you set it to red for the entire attributed string:

let attributes: [NSAttributedString.Key: AnyObject] = 
                  [NSAttributedString.Key.foregroundColor: UIColor.red]
attributedString?.addAttributes(attributes, 
                  range: NSRange.init(location: 0, length: attributedString?.length ?? 0))

If you want it to have a "regular" (= I guess black?) color, just don't do that and delete those lines.

And here's how to set the color for the links in your attributed string:
Change the color of a link in an NSMutableAttributedString

This is the key you'll need to use for setting a different underline color:
NSAttributedString.Key.underlineColor


Edit:

To make it more explicit and put the pieces together — this is what you have to do in order to yield the desired link colors:

textView.linkTextAttributes = [
    .foregroundColor: UIColor.black,
    .underlineColor: UIColor.red
]

(In addition to removing to two lines of code as stated above.)

Upvotes: 5

Omar Chaabouni
Omar Chaabouni

Reputation: 446

If you are using a UITextView, it would be enough to set the tintColor to UIColor.red and remove the following:

let attributes: [NSAttributedString.Key: AnyObject] = [NSAttributedString.Key.foregroundColor: UIColor.red]
attributedString?.addAttributes(attributes, range: NSRange.init(location: 0, length: attributedString?.length ?? 0))

so it would look like this:

public func htmlStyleAttributeText(text: String) -> NSMutableAttributedString? {
    if let htmlData = text.data(using: .utf8) {
        let options: [NSAttributedString.DocumentReadingOptionKey: Any] = [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html, NSAttributedString.DocumentReadingOptionKey.characterEncoding: String.Encoding.utf8.rawValue]
        let attributedString = try? NSMutableAttributedString(data: htmlData, options: options, documentAttributes: nil)
        return attributedString
    }
    return nil
}

//
textView.tintColor = .red
textView.attributedText = htmlStyleAttributeText(text: "random text <a href='http://www.google.com'>http://www.google.com </a> more random text")

Output: enter image description here

Upvotes: 1

Related Questions