duguyihou
duguyihou

Reputation: 33

Why label's height is zero?

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?

Upvotes: 0

Views: 328

Answers (2)

jignesh Vadadoriya
jignesh Vadadoriya

Reputation: 3310

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

Upvotes: 0

vikas prajapati
vikas prajapati

Reputation: 1946

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
}

Upvotes: 1

Related Questions