views:

259

answers:

2

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];
+2  A: 

Set the adjustsFontSizeToFitWidth property to YES.

KennyTM
+1  A: 
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";
prendio2
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.
Sheehan Alam
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.
prendio2