Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find actual number of lines of UILabel?

Tags:

ios

uilabel

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,...

like image 854
nburk Avatar asked Jan 23 '15 11:01

nburk


People also ask

How many lines are there in a default UI label of iOS?

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.

How do you control line spacing in UILabel?

"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."

What is UILabel?

A view that displays one or more lines of informational text.

How do you add margins to UILabel?

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.


2 Answers

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     }  } 
like image 158
Kurt J Avatar answered Sep 30 '22 09:09

Kurt J


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) 
like image 22
ramchandra n Avatar answered Sep 30 '22 08:09

ramchandra n