How to find UILabel's number of Lines

后端 未结 7 689
悲哀的现实
悲哀的现实 2020-11-28 05:35

I displayed the text in UILabel by using wrap method. Now I want to need to find the how many number of line is there in UILabel.

If there is any possible way to fi

相关标签:
7条回答
  • 2020-11-28 06:31
    func getHeight(text:  NSString, width:CGFloat, font: UIFont) -> CGFloat
    {
        let rect = text.boundingRect(with: CGSize.init(width: width, height: CGFloat.greatestFiniteMagnitude), options: ([NSStringDrawingOptions.usesLineFragmentOrigin,NSStringDrawingOptions.usesFontLeading]), attributes: [NSFontAttributeName:font], context: nil)
        return rect.size.height
    }
    

    text: the Label whose height you need to find with the given string

    width: the width of UILabel

    font: UILabel's font

    0 讨论(0)
提交回复
热议问题