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 its numberOfLines
property to 0
, so it will expand to however many lines are necessary. But then, how can I find out how many lines it finally got after I set its text
?
I found similar questions, but none seems to provide a concise answer and it seems to me that it must be really easy to get it without any overhead on juggling around with the boundingRectWithSize
or sizeWithFont
,...
By default when you create a UILabel, the text you set on the label will go in one line and if the text is longer than the width of the label, it will be truncated rather than continued on the next line.
"Short answer: you can't. To change the spacing between lines of text, you will have to subclass UILabel and roll your own drawTextInRect, or create multiple labels."
A view that displays one or more lines of informational text.
Just use a UIView as a superview and define a fixed margin to the label with auto layout. If you're in IB, now is the time to remember the menu Editor -> Embed In -> View. Just select your UILabel first :) This is by far I see as the easiest solution.
None of these worked for me. Below one did,
Swift 4.2:
extension UILabel { func calculateMaxLines() -> Int { let maxSize = CGSize(width: frame.size.width, height: CGFloat(Float.infinity)) let charSize = font.lineHeight let text = (self.text ?? "") as NSString let textSize = text.boundingRect(with: maxSize, options: .usesLineFragmentOrigin, attributes: [NSAttributedString.Key.font: font], context: nil) let linesRoundedUp = Int(ceil(textSize.height/charSize)) return linesRoundedUp } }
Swift 4/4.1:
extension UILabel { func calculateMaxLines() -> Int { let maxSize = CGSize(width: frame.size.width, height: CGFloat(Float.infinity)) let charSize = font.lineHeight let text = (self.text ?? "") as NSString let textSize = text.boundingRect(with: maxSize, options: .usesLineFragmentOrigin, attributes: [.font: font], context: nil) let linesRoundedUp = Int(ceil(textSize.height/charSize)) return linesRoundedUp } }
Swift 3:
extension UILabel { func calculateMaxLines() -> Int { let maxSize = CGSize(width: frame.size.width, height: CGFloat(Float.infinity)) let charSize = font.lineHeight let text = (self.text ?? "") as NSString let textSize = text.boundingRect(with: maxSize, options: .usesLineFragmentOrigin, attributes: [NSFontAttributeName: font], context: nil) let linesRoundedUp = Int(ceil(textSize.height/charSize)) return linesRoundedUp } }
Swift 5 (IOS 12.2)
Get max number of lines required for a label to render the text without truncation.
extension UILabel { var maxNumberOfLines: Int { let maxSize = CGSize(width: frame.size.width, height: CGFloat(MAXFLOAT)) let text = (self.text ?? "") as NSString let textHeight = text.boundingRect(with: maxSize, options: .usesLineFragmentOrigin, attributes: [.font: font], context: nil).height let lineHeight = font.lineHeight return Int(ceil(textHeight / lineHeight)) } }
Get max number of lines can be displayed in a label with constrained bounds. Use this property after assigning text to label.
extension UILabel { var numberOfVisibleLines: Int { let maxSize = CGSize(width: frame.size.width, height: CGFloat(MAXFLOAT)) let textHeight = sizeThatFits(maxSize).height let lineHeight = font.lineHeight return Int(ceil(textHeight / lineHeight)) } }
Usage
print(yourLabel.maxNumberOfLines) print(yourLabel.numberOfVisibleLines)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With