美文网首页
iOS 加载前端标签

iOS 加载前端标签

作者: 90后天道酬勤 | 来源:发表于2021-05-20 17:19 被阅读0次

    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加载标签使用。

    相关文章

      网友评论

          本文标题:iOS 加载前端标签

          本文链接:https://www.haomeiwen.com/subject/nkjvjltx.html