【发布时间】:2017-11-03 11:26:07
【问题描述】:
我正在使用 NSAttributedString 使用以下代码显示 HTML 文本。
do {
let html = "<span style=\"font-family: Montserrat-ExtraLight; font-size: 14; line-height: 1.5;\">\(clinic.profile!)</span>"
let clinicProfile = try NSAttributedString(
data: (html.data(using: String.Encoding.unicode, allowLossyConversion: true)!),
options: [
NSDocumentTypeDocumentAttribute : NSHTMLTextDocumentType
],
documentAttributes: nil
)
contentView.attributedText = clinicProfile
scrollView.addSubview(contentView)
} catch {
print(error)
}
我现在要计算字符串的高度,在HTML中,我使用的是p, strong, em, ul > li, ol > li.等HTML标签
我尝试了以下代码。
let size = CGSize(width: view.frame.width, height: 2000)
let boundingBox = contentView.attributedText.boundingRect(
with: size,
options: [.usesLineFragmentOrigin, .usesFontLeading, .usesDeviceMetrics],
context: nil
)
print(boundingBox.height)
这返回 462,比实际高度小得多,如何正确计算这个高度?
谢谢。
【问题讨论】:
标签: ios swift swift3 frame cgrect