Reputation: 2193
I'm trying to convert a HTML-String coming from my backend into a NSAttributedString
for my textView
, but I want to set the font myself.
Here is an example of a HTML-String coming from my server:
<p><strong>Title</strong></p> <p><strong>Extra info ..</strong></p><p>- Some more info</p> <p>- Contact</p>
Some stackoverflow topics suggest setting the font in the server-backend, however this isn't possible in my situation.
I searched online on how to create the NSAtrributedString
: use a WebView
or a NSAttributedString
, I went with the second for certain reasons. I use the following method to convert my HTML-String into a NSAttributedString:
func setHTMLFromString() -> NSAttributedString{
let attrStr = try! NSMutableAttributedString(
data: self.dataUsingEncoding(NSUnicodeStringEncoding, allowLossyConversion: true)!,
options: [ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],
documentAttributes: nil)
return attrStr
}
This method works, however it overrides the default System font I have set for my textView. So I tried adding the font as an attribute:
func setHTMLFromString() -> NSAttributedString{
let attrStr = try! NSMutableAttributedString(
data: self.dataUsingEncoding(NSUnicodeStringEncoding, allowLossyConversion: true)!,
options: [ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],
documentAttributes: nil)
if #available(iOS 8.2, *) {
attrStr.addAttributes([NSFontAttributeName : UIFont.systemFontOfSize(16.0, weight: UIFontWeightThin),
NSForegroundColorAttributeName: UIColor(rgb: 0x555555)], range: NSMakeRange(0, attrStr.length))
} else {
attrStr.addAttributes([NSFontAttributeName : UIFont.systemFontOfSize(16.0),
NSForegroundColorAttributeName: UIColor(rgb: 0x555555)], range: NSMakeRange(0, attrStr.length))
}
return attrStr
}
Now the font I want to display does show correctly, however all the HTML tags stopped working. Anyone have a solution on how to set the font and keep the HTML tags working?
Upvotes: 4
Views: 3984
Reputation: 19782
If by "not working", you mean that the "strong" text is coming out with the same font as the rest of the text, I think that's just a result of applying a font attribute to the whole string - it ends up overriding the font attributes that were originally set up by the HTML. I don't think NSAttributedString attributes nest in the same way that they do in HTML.
I'm not sure why the attributed string would be overriding the default font set on the textView in the first place, though. I haven't seen that in the case of setting an AttributedString as the title of a button, for example. I think it must be that the conversion from HTML to attributed string is introducing some default font attributes.
Not sure what the best way to fix this is - maybe insert a font style into the HTML pre-conversion? Otherwise, I suppose you could loop over the attributes in the string, and replace/modify any font attributes, but that seems messy.
Upvotes: 1
Reputation: 24476
You could embed a CSS font style in your HTML after you receive the HTML from the server and prior to handing it off to the attributed string. Font details could be dynamically set using string interpolation on the CSS style. You can grab your text view's font to set it. I've done something like this before and it worked, but I don't really love it.
Upvotes: 3