小编Mer*_*nlı的帖子

在 Swift 中使用多行获取 UILabel 高度

我正在尝试计算 UILabel 的高度以使页面可滚动。标签包含用于显示 HTML 内容的属性文本。

我正在使用此函数将我的 HTML 内容获取到 UILabel:

func stringFromHTML( string: String?) -> NSAttributedString
{
    do{
        let pStyle = NSMutableParagraphStyle()
        pStyle.lineSpacing = 4
        pStyle.paragraphSpacingBefore = 10

        let str = try NSMutableAttributedString(data:string!.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true
            )!, options:[NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: NSNumber(unsignedLong: NSUTF8StringEncoding)], documentAttributes: nil)
        str.addAttribute(NSParagraphStyleAttributeName, value: pStyle, range: NSMakeRange(0, str.length))
        str.addAttribute(NSFontAttributeName, value: UIFont(name: "Helvetica Neue", size: 16.0)!, range: NSMakeRange(0, str.length))

        return str
    } catch
    {
        print("html error\n",error)
    }
    return NSAttributedString(string: "")
}
Run Code Online (Sandbox Code Playgroud)

我是新手。所以,我做了一个研究,找到了这个扩展来获得 UILabel 的高度。它返回一个值,但我想这不是真的。我的 UIScrollView 似乎不起作用:

extension UILabel{
    func requiredHeight() -> …
Run Code Online (Sandbox Code Playgroud)

uilabel ios swift swift2

4
推荐指数
1
解决办法
4311
查看次数

标签 统计

ios ×1

swift ×1

swift2 ×1

uilabel ×1