Set UILabel the number of lines depending on the text - Objective C and / or Swift

I am trying to display some random text in a UILabel , and of course I have nothing but its width. Is there a way to set the number of lines of UILabel height and / or depending on the text contained in it?

Thanks;)

+8
source share
5 answers
 myUILabel.numberOfLines = 0; myUILabel.text = @"Pass random text here"; [myUILabel sizeToFit]; 
+8
source

Or (if you need a label height for a specific width and font size, you can calculate it using this):

 func getStringHeight(mytext: String, fontSize: CGFloat, width: CGFloat)->CGFloat { let font = UIFont.systemFontOfSize(fontSize) let size = CGSizeMake(width,CGFloat.max) let paragraphStyle = NSMutableParagraphStyle() paragraphStyle.lineBreakMode = .ByWordWrapping; let attributes = [NSFontAttributeName:font, NSParagraphStyleAttributeName:paragraphStyle.copy()] let text = mytext as NSString let rect = text.boundingRectWithSize(size, options:.UsesLineFragmentOrigin, attributes: attributes, context:nil) return rect.size.height } 

Is it needed for Swift or for Objective-C?

+4
source

You can use the NSString UIKIT Addition method

 - (CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size lineBreakMode:(NSLineBreakMode)lineBreakMode 

to calculate the height. sort of.

 CGSize yourLabelSize = [@"Your very long text" sizeWithFont:yourFont constrainedToSize:CGSizeMake(desiredMaximumWidth, 2000) lineBreakMode:NSLineBreakByWordWrapping]; 

it’s really important to understand the constrainedToSize parameter. You have to go through CGSize with the width desired maximum and maximum possible height . Use the same UIFont with your tag. Do not forget to install

 [yourLabel setNumberOfLines:0]; 

But the method is already deprecated in iOS 7 , so you should use

 - (CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)options attributes:(NSDictionary *)attributes context:(NSStringDrawingContext *)context 

yourLabelSize.height will give you the height

hope this helps you ...

+1
source

For Swift 4.0

 func getStringHeight(mytext: String, fontSize: CGFloat, width: CGFloat)->CGFloat { let font = UIFont.systemFont(ofSize: fontSize) let size = CGSize(width: width, height: CGFloat.greatestFiniteMagnitude) let paragraphStyle = NSMutableParagraphStyle() paragraphStyle.lineBreakMode = .byWordWrapping; let attributes = [NSAttributedStringKey.font:font, NSAttributedStringKey.paragraphStyle:paragraphStyle.copy()] let text = mytext as NSString let rect = text.boundingRect(with: size, options:.usesLineFragmentOrigin, attributes: attributes, context:nil) return rect.size.height } 
+1
source
 let l = UILabel() l.numberOfLines = 0 l.layer.frame.size.width = self.view.frame.width - 40 /*padding(20 + 20)*/ l.font = UIFont(name: "BwModelica-Bold", size: 16.0) l.text = "Random Any length Text!!" let noOfLines = ceil(l.intrinsicContentSize.width) / l.frame.width) let lbl_height = noOfLines * l.intrinsicContentSize.height 

This will be your exact dynamic label height and number of lines. Good coding !!!

0
source

Source: https://habr.com/ru/post/974640/


All Articles