How to adjust and make UILabel width according to the size of the text?

My project has a UILabel with text. The font size is 16pt. The content of the text varies depending on different cases. I hope it can automatically adjust the width of the UILabel to fit the full width of the texts without stretching.

Is it possible?

+48
iphone
Nov 04 '10 at 7:54
source share
8 answers

It is assumed that you have already installed the font:

 label.text = @"some text"; [label sizeToFit]; 

You also need to determine the maximum width and tell your program what to do if sizeToFit gives you a width greater than this maximum.

+109
Nov 04 '10 at 21:37
source share
— -

Here, how to do this, suppose the next messageLabel is the label for which you want the desired effect. Now try a simple line of codes:

  // Set width constraint for label; it actually the width of your UILabel CGFloat constrainedWidth = 240.0f; // Calculate space for the specified string CGSize sizeOfText = [yourText sizeWithFont:yourFont constrainedToSize:CGSizeMake(constrainedWidth, CGFLOAT_MAX) lineBreakMode:UILineBreakModeWordWrap]; UILabel *messageLabel = [[UILabel alloc] initWithFrame:CGRectMake(20,20,constrainedWidth,sizeOfText.height)]; messageLabel.text = yourText; messageLabel.numberOfLines = 0;// This will make the label multiline 
+21
Feb 01 '12 at 6:19 06:19
source share
 NSString *txt1=@"I am here."; CGSize stringsize1 = [txt1 sizeWithFont:[UIFont systemFontOfSize:14]]; [label setFrame:CGRectMake(x,y,stringsize1.width,hieght)]; [label setText:txt1]; 
+13
Nov 21 '12 at 8:36
source share

Here I see three options.

First, make the label size large enough to save any text. This is the simplest, but it does not always work well - it depends on its surrounding views.

Secondly, Label can adapt the font size for longer text ( adjustsFontSizeToFitWidth property). This is often undesirable; different fonts in elements may look ugly.

The last option is to programmatically change the size of the label in accordance with its current text. To calculate the size needed to store text with the current font, use something like the following:

 CGSize textSize = [[someLabel text] sizeWithFont:[someLabel font] forWidth:someLabel.bounds.size.width lineBreakMode:UILineBreakModeWordWrap]; 
+7
Nov 04 '10 at 8:10
source share

If you have already installed your font and its size, and if you have a specific frame, try the following for these two general conditions:

 if (label.text.length > maxCharPerLine) [label setNumberOfLines:0]; // infinite lines else [label setNumberOfLines:1]; // one line only // Adjust your font size to fit your desired width. [label setAdjustsFontSizeToFitWidth:YES]; 
+2
Jun 06 '16 at 15:49
source share

Follow this.

 CGSize stringsize = [yourString sizeWithFont:[UIFont systemFontOfSize:fontSize]]; [label setFrame:CGRectMake(x,y,stringsize.width,height)]; [label setText: yourString]; 
+1
Jun 10 '15 at 12:50
source share

Since the size of WhithFont is depreciating in iOS 7.0, then you are below code

 #define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending) if (SYSTEM_VERSION_LESS_THAN(@"7.0")) { // code here for iOS 5.0,6.0 and so on CGSize fontSize = [itemCat_text sizeWithFont:[UIFont fontWithName:@"Helvetica" size:12]]; } else { // code here for iOS 7.0 fontSize = [itemCat_text sizeWithAttributes: @{NSFontAttributeName: [UIFont fontWithName:@"Helvetica" size:12]}]; } 
+1
Sep 15 '16 at 7:45
source share

Using Auto-Layout:

enter image description here

In ViewController:

 override func viewDidLoad() { super.viewDidLoad() sampleLabel.text = "Electrical Maintenance and Repair" sampleLabel.sizeToFit() } 
+1
Nov 30 '16 at 9:04
source share



All Articles