A web service is giving me some strings with HTML tags like <b></b>, <i></i>
and so on.
I need to convert those strings to attributed text to be able to show them in labels.
This is what I'm using so far:
NSError *err;
NSAttributedString *obj = [[[NSAttributedString alloc] initWithData: [str dataUsingEncoding:NSUTF8StringEncoding]
options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
NSCharacterEncodingDocumentAttribute: [NSNumber numberWithInt:NSUTF8StringEncoding]}
documentAttributes: nil
error: &err] defaultFontSized:KDefaultFontSize];
Then I try to use the resulting string in my label:
label.attributedText = obj;
Well, this doesn't work, for some reason beyond my knowledge.
Any help would be greatly appreciated.
EDIT:
the method "defaultFontSized:" seems to be the culprit. If I don't use it, I can see the attributed text. Now the problem is that method should be used, but I don't know what's wrong with it:
-(NSAttributedString*)defaultFontSized:(CGFloat)size{
NSMutableAttributedString *copy = self.mutableCopy;
[copy addAttribute:NSFontAttributeName value: [UIFont fontWithName:@"HelveticaNeue" size:size] range:self.range];
return copy;
}
EDIT 2:
I've solved the problem adding this line before the code for decoding the attributed string:
str = [NSString stringWithFormat:@"<html><span style=\"font-family: HelveticaNeue; font-size: 16\">%@</span></html>", str ];
I've found this in the link sent from @larme