How does a UILabel's minimumScaleFactor work?

Padin215 picture Padin215 · Feb 1, 2013 · Viewed 51.4k times · Source

I have used minimumFontSize before but that function is now deprecated and i don't quite understand how minimumScaleFactor works.

I want the maximum font size to be 10 and the minimum to be 7.

How can I achieve the re-size down to font size 7 with the scale factor?

UILabel creation:

UILabel *label = [[UILabel alloc] init];
[label setTranslatesAutoresizingMaskIntoConstraints:NO];
label.text =  [labelName uppercaseString];
label.textAlignment = NSTextAlignmentCenter;
label.textColor = [UIColor whiteColor];
label.font = [UIFont fontWithName:HELVETICA_FONT_STYLE_BOLD size:9.5];
label.backgroundColor = [UIColor clearColor];
label.minimumScaleFactor = .1f;

[label addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[label(WIDTH)]"
                                                              options:0
                                                              metrics:@{@"WIDTH" : [NSNumber numberWithFloat:buttonSize.width]}
                                                                views:NSDictionaryOfVariableBindings(label)]];

[contentView addSubview:label];

Answer

Scott picture Scott · Mar 4, 2013

You need to set the label.adjustsFontSizeToFitWidth = YES;