Swift3.0下,UILabel加载html,实现富文本

func layoutWithActivityModel(model:ActivityModel){
        do{
            let attrStr = try NSAttributedString(data: model.title!.data(using: String.Encoding.unicode, allowLossyConversion: true)!, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil)
            let txtAttrStr = try NSAttributedString(data: model.content!.data(using: String.Encoding.unicode, allowLossyConversion: true)!, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil)
            TxTLabel.attributedText = txtAttrStr
            titleLabel.attributedText = attrStr
        }catch let error as NSError {
            print(error.localizedDescription)
        }
        
    }

上述方法是写在UITableViewCell中给cell上的UILabel赋值;


 {  content = "<font color=\"#333333\" style=\"font-size:11pt;font-family:PingFangSC-Light\">11\U6708HR\U3001Diro\U6b63\U54c1\U7f8e\U5986\U6b63\U54c1<font style=\"font-weight:700;font-size:14pt;color:#ea094b;font-family:PingFangSC-Light\">0</font>\U5143\U53ef\U8d2d</font>";
    title = "<font color=\"#333333\" style=\"font-size:12pt;font-weight:700;font-family:PingFangSC-Light\">\U8d85\U503c\U6708\U8d2d</font>";
 }
{ content = "<font color=\"#333333\" style=\"font-size:11pt;font-family:PingFangSC-Light\">\U5f85\U8fd4\U91d1\U989d<font style=\"font-weight:700;font-size:14pt;color:#ea094b;font-family:PingFangSC-Light\">4</font>\U5468\U540e\U53ef\U63d0\U73b0,\U5347\U7ea7\U63d0\U901f<font style=\"font-weight:700;font-size:14pt;color:#ea094b;font-family:PingFangSC-Light\">7</font>\U5929!</font>";
  title = "<font color=\"#333333\"  style=\"font-size:12pt;font-weight:700;font-family:PingFangSC-Light\">\U6781\U901f\U63d0\U73b0</font>";
 }
{content = "<font color=\"#666666\" style=\"font-size:11pt;font-family:PingFangSC-Light\">\U9a6c\U4e0a\U5230\U6765,\U656c\U8bf7\U671f\U5f85</font>";
title = "<font color=\"#333333\"  style=\"font-size:12pt;font-weight:700;font-family:PingFangSC-Light\">\U7cbe\U54c1\U5c1d\U9c9c</font>";
}

以上是下发的html标签


效果图
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • *面试心声:其实这些题本人都没怎么背,但是在上海 两周半 面了大约10家 收到差不多3个offer,总结起来就是把...
    Dove_iOS阅读 27,311评论 30 472
  • 因为要结局swift3.0中引用snapKit的问题,看到一篇介绍Xcode8,swift3变化的文章,觉得很详细...
    uniapp阅读 10,032评论 0 12
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,194评论 4 61
  • 时光如梦,你如画。 梦里花开,零落相思几许。 一纸经年,半笺心语。 浅诉流年似水情, 不思量,意难忘。
    姝瑾儿阅读 2,795评论 47 41