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()
        }
        
  • 相关阅读:
    JS DOM2级事件兼容处理
    JS DOM2级事件基础
    JS 事件基础
    JS 动态规划 LeetCode 一和零
    JS 动态规划入门
    JS 动画优化 代码篇
    LeetCode笔记整理1 摩尔投票法
    LeetCode笔记整理3 二叉树中序遍历 Morris中序遍历
    java面向对象编程——第四章 类和对象
    java面向对象编程——第六章 数组
  • 原文地址:https://www.cnblogs.com/sunyaxue/p/5580990.html
Copyright © 2011-2022 走看看