2

I have UITextview which is assigned to attributed text, It is rendering correctly but its not applying the link colour, in below snippet the link colour is #EB0A1E but in textview it is visible as blue(default link color)

textView.attributedText = "Google.com link <a href = \"https://google.com\" style=color:#EB0A1E;>here</a><br><br> Hello there".htmlToAttributedString

extension String {

    var htmlToAttributedString: NSAttributedString? {
        guard let data = data(using: .utf8) else { return NSAttributedString() }
        do {
            return try NSAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html, .characterEncoding: String.Encoding.utf8.rawValue], documentAttributes: nil)
        } catch {
            return NSAttributedString()
        }
    }
}
Rama Krish
  • 373
  • 1
  • 13

1 Answers1

0

maybe use this answer from @Suragch? It is working for me. I just used different color.

textView with image

  • I cannot hard code the link colour as green in link attributes, It should take the colour from the html itself – Rama Krish Nov 25 '21 at 10:04
  • I suppose you can't use webview for showing HTML? – David Prusa Nov 25 '21 at 10:57
  • @RamaKrish I don't think it is possible to do it by just rendering HTML in textview. Even this library [link](https://github.com/nicolasgoutaland/GONMarkupParser) cant do it: "Color style won't be applied to links. You have to use linkTextAttributes attribute from your UITextView to set it." – David Prusa Nov 25 '21 at 11:20
  • @RamaKrish maybe you can parse color from HTML and then use it as a variable for an answer I proposed? – David Prusa Nov 25 '21 at 11:21
  • we cannot use web view not sure how to parse the html and render – Rama Krish Nov 25 '21 at 15:42