Figure out size of UILabel based on String in Swift

后端 未结 11 1391
执笔经年
执笔经年 2020-11-22 13:35

I am trying to calculate the height of a UILabel based on different String lengths.

func calculateContentHeight() -> CGFloat{
    var maxLabelSize: CGSize         


        
11条回答
  •  有刺的猬
    2020-11-22 14:17

    In Swift 5:

    label.textRect(forBounds: label.bounds, limitedToNumberOfLines: 1)
    

    btw, the value of limitedToNumberOfLines depends on your label's text lines you want.

提交回复
热议问题