How can I make text in a UILabel shrink font size
Asked Answered
N

4

31

If a UILabel contains too much text, how can I setup my label so that it shrinks font-sizes?

Here is how I am setting up my UILabel:

     descriptionLabel = [[UILabel alloc] initWithFrame:CGRectMake(200, 30, 130, 150)];
    [descriptionLabel setFont:[Utils getSystemFontWithSize:14]];
    [descriptionLabel setBackgroundColor:[UIColor clearColor]];
    [descriptionLabel setTextColor:[UIColor whiteColor]];
    descriptionLabel.numberOfLines = 1;
    [self addSubview:descriptionLabel];
Natika answered 21/4, 2010 at 18:55 Comment(0)
S
65
descriptionLabel.adjustsFontSizeToFitWidth = YES;
descriptionLabel.minimumFontSize = 10.0; //adjust to preference obviously

The following example is tested and verified on iPhone Simulator 3.1.2:

UILabel *descriptionLabel = [[UILabel alloc] initWithFrame:CGRectMake(90, 0, 200, 30)];

descriptionLabel.font = [UIFont systemFontOfSize:14.0];
descriptionLabel.minimumFontSize = 10.0;
descriptionLabel.adjustsFontSizeToFitWidth = YES;
descriptionLabel.numberOfLines = 1;
descriptionLabel.text = @"supercalifragilisticexpialidocious even thought he sound of it is something quite attrocious";
Spake answered 21/4, 2010 at 19:2 Comment(4)
I have added those line but it doesn't seem to work. Even when I specify my rect to be something small like CGRectMake(200,30,10,10) nothing happens.Natika
I'm not sure what exactly your [Utils getSystemFontWithSize:] is returning… I'm editing my answer to include an example I've just tested and verified.Spake
As of iOS 6, you should now be using setMinimumScaleFactor instead of minimumFontSize.Carton
Thanks so much. That has just saved me so work and time. @SpakeLancashire
W
22

To resize the text in a multi-line UILabel, you can use this helper method (based on code from 11 Pixel Studios):

+ (void)resizeFontForLabel:(UILabel*)aLabel maxSize:(int)maxSize minSize:(int)minSize { 
 // use font from provided label so we don't lose color, style, etc
 UIFont *font = aLabel.font;

 // start with maxSize and keep reducing until it doesn't clip
 for(int i = maxSize; i >= minSize; i--) {
  font = [font fontWithSize:i];
  CGSize constraintSize = CGSizeMake(aLabel.frame.size.width, MAXFLOAT);

  // This step checks how tall the label would be with the desired font.
  CGSize labelSize = [aLabel.text sizeWithFont:font constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
  if(labelSize.height <= aLabel.frame.size.height)
   break;
 }
 // Set the UILabel's font to the newly adjusted font.
 aLabel.font = font;
}
Windowpane answered 13/12, 2010 at 21:23 Comment(1)
In your for loop, the condition should be i >= minSize, not i > 10Bassarisk
D
6

Set the adjustsFontSizeToFitWidth property to YES.

Discipline answered 21/4, 2010 at 18:58 Comment(0)
F
0

If you want the number of lines to also increase if needed, use Steve N's solution, with the if statement as so:

if(labelSize.height <= aLabel.frame.size.height)
{
  aLabel.numberOfLines = labelSize.height / font.lineHeight;

  break;
}
Fidellia answered 3/9, 2011 at 5:38 Comment(1)
Or just set the number of lines to 0.Artery

© 2022 - 2024 — McMap. All rights reserved.