【问题标题】:Swift 3.0 get the height of html string in UILABELSwift 3.0 获取 UILABEL 中 html 字符串的高度
【发布时间】:2017-06-24 20:42:01
【问题描述】:

我在 UITableView 上有行列表,每行包含来自 apis 的 html 字符串,我正在使用

 let str = try NSMutableAttributedString(data: d,
                                                    options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],
                                                    documentAttributes: nil)

但我无法获得每个 html 字符串的真实高度?我尝试使用 Label.sizeThatFits 但它不起作用。

如何从 UILabel 获取 html 字符串的真实高度?

谢谢

【问题讨论】:

标签: html swift uilabel


【解决方案1】:

试试这个功能

'
func heightForHtmlString(_ text: NSMutableAttributedString, font:UIFont, labelWidth:CGFloat) -> CGFloat{
    let label:UILabel = UILabel.init(frame: CGRect.init(x: 0, y: 0, width: labelWidth, height: CGFloat.greatestFiniteMagnitude))
    label.numberOfLines = 0
    label.lineBreakMode = NSLineBreakMode.byWordWrapping
    label.font = font
    label.attributedText = text as NSAttributedString
    label.sizeToFit()
    return label.frame.height
}'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-30
    • 1970-01-01
    • 1970-01-01
    • 2014-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多