我正在解析来自 Google Direction API 的 JSON 响应,它包含 html_instructions。我想在 UILabel 上显示它。但没有得到任何完美的解决方案来计算 UILabel 所需的高度。
html_说明如下:
Merge onto <b>Shankar Sheth Rd</b><div style=\"font-size:0.9em\">Pass by the gas station (on the left)</div>
我尝试过 stackoverflow 上提到的解决方案,例如使用 sizeThatFits()
和 boundingRectWithSize()
,它给了我正确的 html_instructions 高度没有 <div>
标签,但当 html_instructions 有 <div>
时它会留下额外的空间标签。
请告诉我如何使用包含 html 标签(如 <div>
)的属性文本获取 UILabel 所需的确切高度
最佳答案
我总是使用这个 UILabel
扩展,它也适用于 HTML 标签。没有理由它不应该工作
extension UILabel {
func sizeForString(string: NSString, constrainedToWidth width: Double) -> CGSize {
return string.boundingRectWithSize(
CGSize(
width: width,
height: DBL_MAX
),
options: .UsesLineFragmentOrigin,
attributes: [
NSFontAttributeName : self.font
],
context: nil).size
}
}
关于objective-c - 如何完美计算 UILabel 所需的高度,其中属性字符串包含 HTML 标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36616880/