【iOS开发】UILabel显示html标签的高度计算
2019-04-02 本文已影响0人
文刂Rn
//富文本
let attrStr = try! NSMutableAttributedString(
data: (answer.content.data(using: .unicode, allowLossyConversion: true)!),
options:[.documentType: NSAttributedString.DocumentType.html,
.characterEncoding: String.Encoding.utf8.rawValue],
documentAttributes: nil)
//行高
let paraph = NSMutableParagraphStyle()
paraph.lineSpacing = 8
attrStr.addAttributes([NSAttributedStringKey.paragraphStyle:paraph],
range: NSMakeRange(0, attrStr.length))
self.answerLabel.attributedText = attrStr
//计算高度
let size:CGRect = attrStr.boundingRect(with: CGSize(width: SCREENWIDTH-32, height: CGFloat(MAXFLOAT)), options: NSStringDrawingOptions(rawValue: NSStringDrawingOptions.usesLineFragmentOrigin.rawValue), context: nil)