【Swift3.0】 UILabel 识别HTML,展示照片

1、识别HTML

        let htmlStr =  "<strong>哈哈哈</strong>" 
        do {
            let attrStr =  try NSMutableAttributedString(data: htmlStr.data(using: String.Encoding.utf8, allowLossyConversion: true)!, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,NSCharacterEncodingDocumentAttribute: NSNumber(value: String.Encoding.utf8.rawValue)], documentAttributes: nil)
            self.label.attributedText = attrStr
            
        } catch let error as NSError {
            print(error.localizedDescription)
            self.label.text = ""
        }

2、在UILabel展示图片

        let htmlStr =  "<strong>哈哈哈</strong>"
       
        do {
            let attrStr =  try NSMutableAttributedString(data: htmlStr.data(using: String.Encoding.utf8, allowLossyConversion: true)!, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,NSCharacterEncodingDocumentAttribute: NSNumber(value: String.Encoding.utf8.rawValue)], documentAttributes: nil)
            let attach = NSTextAttachment.init();
            attach.image  = UIImage.init(named: "login_bg")  
            let attachmentString = NSAttributedString.init(attachment: attach)
            attrStr.append(attachmentString)

            self.label.attributedText = attrStr

        } catch let error as NSError {
            print(error.localizedDescription)
            self.label.text = ""
        }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,251评论 4 61
  • 梦里我梦见我和自己谈了个恋爱,很开心,一起逛了街,一起吃了饭。她懂我如我懂我一样,后来她说你看,我不是我,我是你。...
    菜园子家的狗尾草阅读 107评论 0 1
  • 毕业的日子越来越近,楼道里的行李轮子的滚动声越来越多,同学们也都逐渐返校了。每当听到楼道里的声音,心里总是在想,哦...
    木槿autumn阅读 153评论 0 0