swift4 label显示html,swift label加载html预估高度

注意:如果html中包含图片链接则预估不准确.

+ (NSStringDrawingOptions)stringDrawingOptions{

return NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading;

}

*options参数由于在swift中没找到正确的写法,所以我是oc类中定义了实现options多选的方法

do{

let htmlString = contentArry[0] as! String

let attributedStr = try NSMutableAttributedString(data: htmlString.dataUsingEncoding(NSUnicodeStringEncoding,allowLossyConversion: true)!, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil)

let options : NSStringDrawingOptions = H5WebView.stringDrawingOptions()

attributedStr.addAttribute(NSDocumentTypeDocumentAttribute, value:UIFont.systemFontOfSize(16), range:NSMakeRange(0, attributedStr.length))

let rect = attributedStr.boundingRectWithSize(CGSizeMake(MAIN_SCREEN_WIDTH-60, 250), options: options, context: nil)//UsesLineFragmentOrigin

let htmlHeight = CGRectGetHeight(rect)

return htmlHeight

}catch let error as NSError {

yqLog(error.localizedDescription)

return 160

}catch {//加入一个空的catch,用于关闭catch。否则会报错:Errors thrown from here are not handled because the enclosing catch is not exhaustive

return 160

}

```

*后期有空的时候我会描述一个昨天填的坑:在tableView自定义cell用使用webView加载html,利用webView预加载获取webView的高,适配cell的高.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值