How to find actual number of lines of UILabel?

前端 未结 14 2386
灰色年华
灰色年华 2020-11-27 13:10

How can I find the actual number of lines of a UILabel after I have initialized it with a text and a font? I have set

14条回答
  •  眼角桃花
    2020-11-27 13:40

    let l = UILabel()
    l.numberOfLines = 0
    l.layer.frame.size.width = self.view.frame.width - 40 /*padding(20 + 20)*/
    
    l.font = UIFont(name: "BwModelica-Bold", size: 16.0)
    
    l.text = "Random Any length Text!!"
    
    let noOfLines = ceil(l.intrinsicContentSize.width) / l.frame.width)
    
    let lbl_height = noOfLines * l.intrinsicContentSize.height
    

    This will be your Exact dynamic height of Label and Number of lines. Happy coding!!!

提交回复
热议问题