前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >iOS Label加载HTML

iOS Label加载HTML

作者头像
码客说
发布2019-10-22 14:44:36
1.7K0
发布2019-10-22 14:44:36
举报
文章被收录于专栏:码客码客

Label加载富文本

代码语言:javascript
复制
func getHtml(){
    guard let filePath = Bundle.main.path(forResource: "demo", ofType: "html") else{return}
    guard let data = NSData.init(contentsOfFile: filePath) as Data? else {return}
    guard let contentStr = String.init(data: data, encoding: String.Encoding.utf8) else{return}

    do{
        let attrStr = try NSAttributedString.init(data: contentStr.data(using: String.Encoding.unicode)!, options: [NSAttributedString.DocumentReadingOptionKey.documentType : NSAttributedString.DocumentType.html], documentAttributes: nil)
        self.htmlLabel.attributedText = attrStr
    }catch{
       print(error.localizedDescription)
    }
}

允许进行HTTP请求 加载网络图片

  • Info.plist中添加NSAppTransportSecurity类型Dictionary
  • NSAppTransportSecurity下添加NSAllowsArbitraryLoads类型Boolean,值设为YES

或者直接添加一下配置

代码语言:javascript
复制
<key>NSAppTransportSecurity</key>
<dict>
	<key>NSAllowsArbitraryLoads</key>
	<true/>
</dict>

注意点

  • 加载图片的大小 不是按手机的像素尺寸 而是手机的尺寸(比如width:414px就是最大的宽度)
  • 图片无法添加点击操作 链接无法点击

猿题库作者的实现方式

iOS 开发进阶

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-03-07,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Label加载富文本
  • 允许进行HTTP请求 加载网络图片
  • 注意点
  • 猿题库作者的实现方式
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档