Reputation: 16841
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.
Upvotes: 1
Views: 8330
Reputation: 49
You should used following code. it's help you!! Apple provided document and link for NSAttributedString.
https://developer.apple.com/documentation/foundation/nsattributedstring/1524613-initwithdata
NSString * htmlString = @"You added a note - in <a href='http://localhost:80/ABC/phase2/public/users/Test/notes/5'>hfgh</a>";
NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
myLabel.attributedText = attrStr;
Upvotes: 2
Reputation: 996
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
Upvotes: 5