Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fit text in UILabel

Here is my code

 UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 50, 300, 50)];     label.textAlignment = UITextAlignmentCenter;     label.backgroundColor = [UIColor clearColor];     label.textColor = [UIColor whiteColor];     label.textColor.font = [UIFont fontWithName:@"Verdana" size:30];     label.text = @"A very long string";      etc... 

The problems is that the font is large and can't fit in the label. It just display "A very"

What to do so entire text to be displayed. I have tried

label.lineBreakMode = UILineBreakModeWordWrap; label.numberOfLines = 0; 

But it doesn't work for me. I want to do that programmatically.

//EDIT

CGRect frame = CGRectMake(10, 50, 300, 50);     NSString *labelString = @"Players.";      UILabel *howManyUsersLabel = [[UILabel alloc]initWithFrame:frame];     howManyUsersLabel.textAlignment = UITextAlignmentCenter;     howManyUsersLabel.backgroundColor = [UIColor clearColor];     howManyUsersLabel.textColor = [UIColor whiteColor];     howManyUsersLabel.adjustsFontSizeToFitWidth = NO;     howManyUsersLabel.numberOfLines = 0;      CGFloat fontSize = 30;     while (fontSize > 0.0)     {         CGSize size = [labelString sizeWithFont:[UIFont fontWithName:@"Verdana" size:fontSize] constrainedToSize:CGSizeMake(frame.size.width, 10000) lineBreakMode:UILineBreakModeWordWrap];          if (size.height <= frame.size.height) break;         fontSize -= 1.0;         NSLog(@"test");     }      howManyUsersLabel.font = [UIFont fontWithName:@"Verdana" size:fontSize]; 
like image 310
objlv Avatar asked Feb 01 '12 19:02

objlv


People also ask

How do you change the width of a UILabel?

To change the font or the size of a UILabel in a Storyboard or . XIB file, open it in the interface builder. Select the label and then open up the Attribute Inspector (CMD + Option + 5). Select the button on the font box and then you can change your text size or font.

How to increase label size according to text in iOS?

Just use auto layout to add constraints to pin the left and top sides of the label. After that it will automatically resize.

How do I change the size of labels in Xcode?

You can set the Minimum Font Scale or size in Storyboard/Xib when you set it up in IB under the Attributes inspector. I prefer scale, as it is better at fitting longer text on iPhone 4/5/iPod touches. If you set the size, you can get cut off earlier than with scale.


2 Answers

I think you just need to add this:

label.adjustsFontSizeToFitWidth = YES; label.minimumFontSize = 0; 

Then the text will automatically resize to fit the label.

Note however that this will only really work if the label.numberOfLines = 1, so that the text is on a single line.

If you need the text to wrap onto multiple lines but still shrink to fit, the solution is more complex. To do this, you need to calculate the rendered size of the text and then reduce it in a loop, as follows:

NSString *theText = @"A long string"; CGRect labelRect = CGRectMake(10, 50, 300, 50); label.adjustsFontSizeToFitWidth = NO; label.numberOfLines = 0;  CGFloat fontSize = 30; while (fontSize > 0.0) {     CGSize size = [theText sizeWithFont:[UIFont fontWithName:@"Verdana" size:fontSize] constrainedToSize:CGSizeMake(labelRect.size.width, 10000) lineBreakMode:UILineBreakModeWordWrap];      if (size.height <= labelRect.size.height) break;      fontSize -= 1.0; }  //set font size label.font = [UIFont fontWithName:@"Verdana" size:fontSize]; 

This basically just reduces the font size until it fits the label.

UPDATE:

As of iOS7, multiline text will also shrink automatically when adjustsFontSizeToFitWidth = YES, so the second part of this answer is no longer needed (unless you still support iOS 6 and earlier).

like image 109
Nick Lockwood Avatar answered Sep 21 '22 19:09

Nick Lockwood


Finally I got solution for text allignment issue in arabic language you just do like this:

    label.text = @"هذا هو نص طويل جدا";     label.textAlignment = NSTextAlignmentNatural;      CGSize size = [labels sizeThatFits:CGSizeMake(_lblAddress.width, CGFLOAT_MAX)];     label.height = size.height; 
like image 28
Maishi Wadhwani Avatar answered Sep 23 '22 19:09

Maishi Wadhwani