iOS 加载前端标签

2021-05-20  本文已影响0人  90后天道酬勤

1、UILabel加载标签

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];

        

lable富文本

2、UITextView加载标签

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];

textView富文本

3、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显示图 对比结果

总结:建议使用wkwebView加载标签使用。

上一篇下一篇

猜你喜欢

热点阅读