zoukankan      html  css  js  c++  java
  • swift UILabel加载html源码

      @IBOutlet weak var content: UILabel!
    
        func setup(content:String){
            self.content.preferredMaxLayoutWidth = UIScreen.mainScreen().bounds.size.width - 36
            let content = "<html><head><meta name= "viewport" content= "width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0"></head><body>" + content + "</body></html>"
            let data:NSData? = content.dataUsingEncoding(NSUnicodeStringEncoding, allowLossyConversion: true)
            
            do {
                
                let attrStr = try NSMutableAttributedString(data: data!, options: [NSDocumentTypeDocumentAttribute : NSHTMLTextDocumentType], documentAttributes: nil)
                
                attrStr.enumerateAttribute(NSAttachmentAttributeName, inRange: NSMakeRange(0, attrStr.length), options: NSAttributedStringEnumerationOptions.Reverse, usingBlock: { (vale, range, stop) -> Void in
                    if let ment = vale as? NSTextAttachment{
                        ment.bounds.size = CGSizeMake(UIScreen.mainScreen().bounds.size.width - 20, 130)
                    }
                })
                
                attrStr.addAttribute(NSFontAttributeName, value: UIFont.systemFontOfSize(14), range: NSMakeRange(0, attrStr.length))
                // 设置行距
                let style = NSMutableParagraphStyle()
                style.lineSpacing = 5
                attrStr.addAttribute(NSParagraphStyleAttributeName, value: style, range: NSMakeRange(0, attrStr.length))
                self.content.attributedText = attrStr
            } catch{
                
            }
            self.content.font = UIFont.systemFontOfSize(14)
            self.content.textColor = UIColor.darkGrayColor()
        }
        
  • 相关阅读:
    项目跟踪管理三个部分
    Element-UI树形结构表格的操作
    简化代码的小知识点
    swiper在vue中正确的使用方法
    如何创建一个新的vue项目
    前端开发常用代码片段(下篇)
    前端开发常用代码片段(中篇)
    前端开发常用代码片段(上篇)
    移动端h5实现复制功能
    最全的页面初始化样式
  • 原文地址:https://www.cnblogs.com/sunyaxue/p/5580990.html
Copyright © 2011-2022 走看看