Illep Illep - 3 months ago 13
iOS Question

How to show an HTML string on a UILabel

I have a

UILabel
. And I want to set a string that also contains some
HTML
tags in it. The string is:

NSString *str = @"<p>Hey you. My <b>name </b> is <h1> Joe </h1></p>";


How can i display this on the
UILabel


NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[str dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];



mylabel.attributedText=attrStr;


The above code doesn't display anything.

Answer

For iOS7 or more you can use this:

NSString * htmlString = @" <p><b>Hey</b> you. My <b>name </b> is <h1> Joe </h1></p> ";
    NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];


UILabel * myLabel = [UILabel alloc] init];
myLabel.attributedText = attrStr;

Its works

Comments