How to calculate TextView height base on text

前端 未结 6 2025
無奈伤痛
無奈伤痛 2021-01-05 02:43

I am using the code below for calculate the height of text, then set this height for UILabel and UITextView

CGSize targetSize = CGS         


        
6条回答
  •  悲&欢浪女
    2021-01-05 03:13

    This calculates the size of any string, whether or not you put them in a text view.

    let frame = NSString(string: yourText).boundingRect(
        with: CGSize(width: yourDesiredWidth, height: .infinity),
        options: [.usesFontLeading, .usesLineFragmentOrigin],
        attributes: [.font : yourFont],
        context: nil)
    
    let height = frame.size.height
    

提交回复
热议问题