objlv
objlv

Reputation: 601

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];

Upvotes: 38

Views: 43809

Answers (7)

Suragch
Suragch

Reputation: 511736

Swift with iOS 9

let maxFontSize: CGFloat = 40
let minFontSize: CGFloat = 10

label.font = UIFont(name: label.font.fontName, size: maxFontSize)!
label.adjustsFontSizeToFitWidth = true
label.minimumScaleFactor = minFontSize/maxFontSize

This doesn't increase the font size to fill the label. It just starts with the max size and decreases as necessary down to the minimum. This is also assuming that the number of lines is 1.

Upvotes: 1

Nick Lockwood
Nick Lockwood

Reputation: 40995

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

Upvotes: 89

brandonscript
brandonscript

Reputation: 72885

Everything seems to be broken in iOS 8 (probably iOS 7 too).

Solution:

-(UIFont*)fontForString:(NSString*)string toFitInRect:(CGRect)rect seedFont:(UIFont*)seedFont {
    UIFont* returnFont = seedFont;
    CGSize stringSize = [string sizeWithAttributes:@{NSFontAttributeName : seedFont}];

    while(stringSize.width > rect.size.width){
        returnFont = [UIFont systemFontOfSize:returnFont.pointSize -1];
        stringSize = [string sizeWithAttributes:@{NSFontAttributeName : returnFont}];
    }

    return returnFont;
}

Make sure you don't try and use label.adjustsFontSizeToFitWidth = YES otherwise it'll get really confused and the new size won't work properly.

Upvotes: 0

Mahesh Kumar
Mahesh Kumar

Reputation: 1104

[UILabel sizeToFit];

It will work for your problem.

Upvotes: 1

Mahesh Kumar
Mahesh Kumar

Reputation: 1104

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;

Upvotes: 3

Jeffrey Sun
Jeffrey Sun

Reputation: 8049

Interface Builder lets you do this now.

  1. In UILabel, under Autoshrink, select "Minimum Font Size" instead of "Fixed Font Size".
  2. Set the Minimum Font Size to be something reasonable, like 8.
  3. You can also check the checkmark "Tighten Letter Spacing".

Alternatively you can do it programmatically:

label.adjustsFontSizeToFitWidth = YES;

Upvotes: 0

Piyush Kashyap
Piyush Kashyap

Reputation: 1965

CGRect titleRect        = CGRectMake(10, 50, 300, 50);

UILabel *textTitleView  = [[UILabel alloc] initWithFrame:titleRect];

textTitleView.numberOfLines = 3  //for multiple lines;

textTitleView.lineBreakMode = UILineBreakModeWordWrap;

[UIFont fontWithName:@"Verdana" size:30];

textTitleView.text          = @"your text";

Upvotes: 0

Related Questions