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

I use SnapKit to set top and left.

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

It prints 0. Does it all right?

duguyihou
  • 33
  • 3
  • 9

2 Answers2

1

you can get label height by this function in 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
}
vikas prajapati
  • 1,868
  • 2
  • 18
  • 26
0

You can try this,i think it will help you

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

Hope it will help you

jignesh Vadadoriya
  • 3,244
  • 3
  • 18
  • 29