Figure out size of UILabel based on String in Swift

后端 未结 11 1389
执笔经年
执笔经年 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:28

    Check label text height and it is working on it

    let labelTextSize = ((labelDescription.text)! as NSString).boundingRect(
                    with: CGSize(width: labelDescription.frame.width, height: .greatestFiniteMagnitude),
                    options: .usesLineFragmentOrigin,
                    attributes: [.font: labelDescription.font],
                    context: nil).size
                if labelTextSize.height > labelDescription.bounds.height {
                    viewMoreOrLess.hide(byHeight: false)
                    viewLess.hide(byHeight: false)
                }
                else {
                    viewMoreOrLess.hide(byHeight: true)
                    viewLess.hide(byHeight: true)
    
                }
    

提交回复
热议问题