Bob-ob
Bob-ob

Reputation: 1618

Adding and Editing a Dynamic UILabel Objective C

Hey I am having some difficulty adding a dynamic label to my uiview subclass and have it scale itself properly. Here is the code I am using currently:

- (id)initWithFrame:(CGRect)frame
{
   frame = CGRectMake(0, 0, 100, 100);
   CGRect lblFrame = CGRectMake(0, 0, 100,100);
   self = [super initWithFrame:frame];
   if (self) {
    // Initialization code
    testLbl = [[UILabel alloc] initWithFrame:lblFrame];
    testLbl.backgroundColor = [UIColor clearColor];
    testLbl.textColor = [UIColor darkGrayColor];
    testLbl.textAlignment = UITextAlignmentCenter;
    [testLbl setText:@"T"];
    testLbl.numberOfLines = 1;
    testLbl.minimumFontSize = 50;
    testLbl.adjustsFontSizeToFitWidth = YES;
    [self addSubview:testLbl];
    }
return self;
}

This adds the label to the uiview but its text does not adjust. I have tried everything at the moment. Has anybody got any ideas?

Upvotes: 1

Views: 720

Answers (2)

MusiGenesis
MusiGenesis

Reputation: 75296

According to this answer, the .minimumFontSize property is ignored if the text already fits within the label with the current font. Try setting the label font explicitly like this:

testLabel.font = [UIFont systemFontOfSize:50];

Upvotes: 2

jbat100
jbat100

Reputation: 16827

You are setting

testLbl.minimumFontSize = 50;

which is a ridiculously big font (one letter probably does not fit), so it probably tries to adjust but can't actually show anything because of your high minimum font size. Try something like

testLbl.minimumFontSize = 14;

I don't know what the behaviour is if the minimumFontSize is more that the actual specified font size, maybe it just gives up...

Upvotes: 1

Related Questions