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
}
}