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()
        }
        
  • 相关阅读:
    C# 比较多个数组(lambda,匿名比较器)
    JPA javax.persistence.TransactionRequiredException
    JPA:identifier of an instance of was altered from
    JPA 连表查询
    java json转换(二)
    java querydsl使用
    SpringCloud 过滤器
    Android : Resource is not a Drawable (color or path)
    springcloud 新增微服务
    序列化二叉树
  • 原文地址:https://www.cnblogs.com/sunyaxue/p/5580990.html
Copyright © 2011-2022 走看看