如何在UILabel上显示HTMLstring
我有一个UILabel
。 我想设置一个string,其中也包含一些HTML
标签。 string是:
NSString *str = @"<p>Hey you. My <b>name </b> is <h1> Joe </h1></p>";
我如何显示这个UILabel
NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[str dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil]; mylabel.attributedText=attrStr;
上面的代码不显示任何东西。
对于iOS7或更多,你可以使用这个:
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;
其作品
你应该使用下面的代码。 这是帮助你! Apple为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;