您的位置:首页 > 移动开发 > IOS开发

iOS 显示HTML文本

2016-04-25 13:40 471 查看
1、用webView显示

-(void)loadWebViewString:(NSString *)loadString
{
NSString *content = [[NSString alloc] init];
if (loadString == nil || loadString == NULL) {
content =@"";
}else
content =[NSString stringWithFormat:
@"<html>"
"<style type=\"text/css\">"
"<!--"
"body{font-size:12pt;line-height:20pt;}"
"-->"
"</style>"
"<body>"
"%@"
"</body>"
"</html>"
, loadString];
[_webView loadHTMLString:content baseURL:nil];
}


2、用Label显示

NSString * htmlString = @"<html><body> Some html string \n <font size=\"13\" color=\"red\">This is some text!</font> </body></html>";
NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
UILabel * myLabel = [[UILabel alloc] initWithFrame:self.view.bounds];
myLabel.attributedText = attrStr;
[self.view addSubview:myLabel];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  uilabel