1、UILabel加载标签
lable富文本NSString *str = @"<p>高于90分的名称</p>\n<table style=\"border-collapse: collapse; width: 100%; height: 88px;\" border=\"1\">\n<tbody>\n<tr style=\"height: 22px;\">\n<td style=\"width: 22.984015804597703%; height: 22px;\">名称</td>\n<td style=\"width: 22.984015804597703%; height: 22px;\">班级</td>\n<td style=\"width: 22.984015804597703%; height: 22px;\">年龄</td>\n<td style=\"width: 22.984015804597703%; height: 22px;\">成绩</td>\n</tr>\n<tr style=\"height: 22px;\">\n<td style=\"width: 22.984015804597703%; height: 22px;\">小李</td>\n<td style=\"width: 22.984015804597703%; height: 22px;\">1年级</td>\n<td style=\"width: 22.984015804597703%; height: 22px;\">8岁</td>\n<td style=\"width: 22.984015804597703%; height: 22px;\">76</td>\n</tr>\n<tr style=\"height: 22px;\">\n<td style=\"width: 22.984015804597703%; height: 22px;\">小凤</td>\n<td style=\"width: 22.984015804597703%; height: 22px;\">1年级</td>\n<td style=\"width: 22.984015804597703%; height: 22px;\">8岁</td>\n<td style=\"width: 22.984015804597703%; height: 22px;\">92</td>\n</tr>\n<tr style=\"height: 22px;\">\n<td style=\"width: 22.984015804597703%; height: 22px;\">小名</td>\n<td style=\"width: 22.984015804597703%; height: 22px;\">1年级</td>\n<td style=\"width: 22.984015804597703%; height: 22px;\">8岁</td>\n<td style=\"width: 22.984015804597703%; height: 22px;\">91</td>\n</tr>\n</tbody>\n</table>";
NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[str dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(50, 100, 300, 400)];
label.attributedText = attrStr;
label.numberOfLines = 0;
[self.view addSubview:label];
2、UITextView加载标签
textView富文本NSString *str = @"<p>高于90分的名称</p>\n<table style=\"border-collapse: collapse; width: 100%; height: 88px;\" border=\"1\">\n<tbody>\n<tr style=\"height: 22px;\">\n<td style=\"width: 22.984015804597703%; height: 22px;\">名称</td>\n<td style=\"width: 22.984015804597703%; height: 22px;\">班级</td>\n<td style=\"width: 22.984015804597703%; height: 22px;\">年龄</td>\n<td style=\"width: 22.984015804597703%; height: 22px;\">成绩</td>\n</tr>\n<tr style=\"height: 22px;\">\n<td style=\"width: 22.984015804597703%; height: 22px;\">小李</td>\n<td style=\"width: 22.984015804597703%; height: 22px;\">1年级</td>\n<td style=\"width: 22.984015804597703%; height: 22px;\">8岁</td>\n<td style=\"width: 22.984015804597703%; height: 22px;\">76</td>\n</tr>\n<tr style=\"height: 22px;\">\n<td style=\"width: 22.984015804597703%; height: 22px;\">小凤</td>\n<td style=\"width: 22.984015804597703%; height: 22px;\">1年级</td>\n<td style=\"width: 22.984015804597703%; height: 22px;\">8岁</td>\n<td style=\"width: 22.984015804597703%; height: 22px;\">92</td>\n</tr>\n<tr style=\"height: 22px;\">\n<td style=\"width: 22.984015804597703%; height: 22px;\">小名</td>\n<td style=\"width: 22.984015804597703%; height: 22px;\">1年级</td>\n<td style=\"width: 22.984015804597703%; height: 22px;\">8岁</td>\n<td style=\"width: 22.984015804597703%; height: 22px;\">91</td>\n</tr>\n</tbody>\n</table>";
UITextView *textView = [[UITextView alloc]initWithFrame:CGRectMake(0, 50, self.view.frame.size.width, 300)];
textView.userInteractionEnabled = NO;
NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[str dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
textView.attributedText = attrStr;
textView.font = [UIFont systemFontOfSize:14];
[self.view addSubview:textView];
3、wkwebView加载标签
wkwebView显示图 对比结果NSString *str = @"<p>高于90分的名称</p>\n<table style=\"border-collapse: collapse; width: 100%; height: 88px;\" border=\"1\">\n<tbody>\n<tr style=\"height: 22px;\">\n<td style=\"width: 22.984015804597703%; height: 22px;\">名称</td>\n<td style=\"width: 22.984015804597703%; height: 22px;\">班级</td>\n<td style=\"width: 22.984015804597703%; height: 22px;\">年龄</td>\n<td style=\"width: 22.984015804597703%; height: 22px;\">成绩</td>\n</tr>\n<tr style=\"height: 22px;\">\n<td style=\"width: 22.984015804597703%; height: 22px;\">小李</td>\n<td style=\"width: 22.984015804597703%; height: 22px;\">1年级</td>\n<td style=\"width: 22.984015804597703%; height: 22px;\">8岁</td>\n<td style=\"width: 22.984015804597703%; height: 22px;\">76</td>\n</tr>\n<tr style=\"height: 22px;\">\n<td style=\"width: 22.984015804597703%; height: 22px;\">小凤</td>\n<td style=\"width: 22.984015804597703%; height: 22px;\">1年级</td>\n<td style=\"width: 22.984015804597703%; height: 22px;\">8岁</td>\n<td style=\"width: 22.984015804597703%; height: 22px;\">92</td>\n</tr>\n<tr style=\"height: 22px;\">\n<td style=\"width: 22.984015804597703%; height: 22px;\">小名</td>\n<td style=\"width: 22.984015804597703%; height: 22px;\">1年级</td>\n<td style=\"width: 22.984015804597703%; height: 22px;\">8岁</td>\n<td style=\"width: 22.984015804597703%; height: 22px;\">91</td>\n</tr>\n</tbody>\n</table>";
WKWebView *webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, 300)];
[webView loadHTMLString:str baseURL:nil];
[self.view addSubview:webView];
总结:建议使用wkwebView加载标签使用。
网友评论