Search code examples
iosswiftuilabel

Inconsistent behavior with number of actual lines calculation for UILabel in IOS with Swift


I am using the below UILabel extension method to calculate the number of actual lines in the UILabel. However, I see that it always returns more than the actual number of lines. Moreover, the excess number of lines it returns is not the same always. So, I cannot subtract it with a fixed constant before using the value. Any thoughts of what is wrong here. I have already looked at the solutions posted in stack overflow but they did not help either.

extension UILabel {
    var maxNumberOfLines: Int {
        guard let text = text, let font = font else {
            return 0
        }
        let charSize = font.lineHeight
        let textSize = (text as NSString).boundingRect(
        with: CGSize(width: frame.width, height: .greatestFiniteMagnitude),
        options: .usesLineFragmentOrigin,
        attributes: [.font: font],
            context: nil)
        let linesRoundedUp = Int(ceil(textSize.height/charSize))
        return linesRoundedUp
    }

}

Solution

  • Since I was trying to find the number of lines for UILabel in cell for row at index path delegate method, I was getting wrong result as the UILabel would not be laid out in the tableview cell yet. I used UIScreen.main.bounds - some offset in place of label width for number of lines calculation and that solved the issue.

    To be precise, I used

    extension UILabel {
        var maxNumberOfLines: Int {
            guard let text = text, let font = font else {
                return 0
            }
            let charSize = font.lineHeight
            let textSize = (text as NSString).boundingRect(
            with: CGSize(width: UIScreen.main.bounds.width - SOMEOFFSET, height: .greatestFiniteMagnitude),
            options: .usesLineFragmentOrigin,
            attributes: [.font: font],
                context: nil)
            let linesRoundedUp = Int(ceil(textSize.height/charSize))
            return linesRoundedUp
        }
    
    }