0
contentLabel.preferredMaxLayoutWidth = kScreenWidth - 20
contentLabel.numberOfLines = 0

我使用 SnapKit 设置顶部和左侧。

 contentLabel.snp.makeConstraints { (make) in
  make.top.equalTo(topView.snp.bottom)
  make.left.equalTo(topView.iconView)
}
print(contentLabel.frame.size.height)

它打印 0. 可以吗?

4

2 回答 2

1

您可以在 Swift 3 中通过此函数获取标签高度:

func GetheightOfLable(strLable:String , widthlable:CGFloat) -> CGFloat{

    let rectDetail:CGRect = strLable.boundingRect(with: CGSize(width: widthlable, height: CGFloat(MAXFLOAT)), options: NSStringDrawingOptions.usesLineFragmentOrigin, attributes: [NSFontAttributeName:UIFont.systemFontSize], context: nil)
    print(rectDetail.height)
    return rectDetail.height
}
于 2016-12-12T04:12:50.620 回答
0

你可以试试这个,我想它会帮助你

let sizeToFit = CGSize(width: contentLabel.frame.size.width,
                       height: CGFloat.greatestFiniteMagnitude)
let textSize = contentLabel.sizeThatFits(sizeToFit)

希望对你有帮助

于 2016-12-12T05:31:11.760 回答