I have a UITextView and I have a string of HTML I give it like so...
func applyHTML() {
textView.attributedText = htmlString()
textView.linkTextAttributes = [NSForegroundColorAttributeName: .blue,
NSUnderlineStyleAttributeName: NSUnderlineStyle.StyleSingle.rawValue,
NSFontAttributeName:font]
}
func htmlString() -> NSAttributedString? {
if let htmlData = text.dataUsingEncoding(NSUnicodeStringEncoding) {
do {
let attributedString = try NSMutableAttributedString(data: htmlData,
options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],
documentAttributes: nil)
return attributedString
}
catch { return nil }
}
return nil
}
This works fine, I get blue hyperlinks with black text... what I want to do now is style the font for all and colour of the non-hyperlink text... problem is when I do that, by adding this....
let length = attributedString.length
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .Center
attributedString.setAttributes([NSForegroundColorAttributeName:.white,NSFontAttributeName: myFont,NSParagraphStyleAttributeName: paragraphStyle], range: NSRange(location: 0, length: length))
it removes the hyperlinks... I want them to work in tandem. What am I doing wrong?
For anyone who visits, @Larme was right
attributedString.setAttributes([NSForegroundColorAttributeName:.white,NSFontAttributeName: myFont,NSParagraphStyleAttributeName: paragraphStyle], range: NSRange(location: 0, length: length))
should be
attributedString.addAttributes([NSForegroundColorAttributeName:.white,NSFontAttributeName: myFont,NSParagraphStyleAttributeName: paragraphStyle], range: NSRange(location: 0, length: length))