How to find actual number of lines of UILabel?

前端 未结 14 2408
灰色年华
灰色年华 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:44

    Here is the Swift3 Code here you can define Int value and get the height of text size by using (MAXFLOAT) and using that height you can get the total height of UILabel and by deviding that total height by character size you can get the actual line count of UILabel.

    var lineCount: Int = 0
    var textSize = CGSize(width: CGFloat(yourLabel.frame.size.width), height: CGFloat(MAXFLOAT))
    var rHeight: Int = lroundf(yourLabel.sizeThatFits(textSize).height)
    var charSize: Int = lroundf(yourLabel.font.leading)
    lineCount = rHeight / charSize
    print("No of lines: \(lineCount)")
    

提交回复
热议问题