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()
        }
        
  • 相关阅读:
    决策树
    flask数据库的迁移
    flask的查询,一对多,多对多
    django的验证码
    第一讲:python环境配置(python2.7+python3.7)
    pytest
    【TS】-类型兼容性
    HTML--src和href的区别
    JS--为什么null是对象?
    JS--null和undefined的区别
  • 原文地址:https://www.cnblogs.com/sunyaxue/p/5580990.html
Copyright © 2011-2022 走看看