Im using a UITextView to display HTML content which works fine except links. I can set font and color for normal text like this:
let str = "<font size='4' style='color:red'>text is red</font>"
which works just fine. But when trying to color the link like this nothing happens.
let str = "some text <a href='http://stackoverflow.com' style='color:red'>
link is not red!?</a> some text <a href='http://google.com'
style='color:green'> link is not green!?</a>"
This is how I set the string to the UITextView.
var attrStr = NSAttributedString(
data: str.dataUsingEncoding(NSUnicodeStringEncoding, allowLossyConversion: true)!,
options: [ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],
documentAttributes: nil,
error: nil)
myTextView.attributedText = attrStr
EDIT:
Can edit all link colors with this code. Not possible to have different colors depending on the link though, which was what I was looking for.
myTextView.linkTextAttributes = [NSForegroundColorAttributeName: UIColor.greenColor()]