How does a UILabel's minimumScaleFactor work?
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];
Solution 1:
You need to set the label.adjustsFontSizeToFitWidth = YES;
Solution 2:
In addition to what the other answers say, if you put minSize/defaultSize (division) as the minimumScaleFactor
, it will be the same as using the old minimumFontSize
.
Ex, if you want the minimum font size to be 10 using default label size, you can do:
[label setMinimumScaleFactor:10.0/[UIFont labelFontSize]];
(Replace [UIFont labelFontSize]
with your label's font size if it is not the default).
which would be the same as:
[label setMinimumFontSize:10.0];
Solution 3:
According to the documentation:
Use this property to specify the smallest multiplier for the current font size that yields an acceptable font size to use when displaying the label’s text. If you specify a value of 0 for this property, the current font size is used as the smallest font size.
So if default font size for your label is 10
, you put 0.7f
as a minimumScaleFactor
and it should do the same thing as minimumFontSize
did.
Solution 4:
In addition to other answers, I'm going to add a beginner-friendly explanation that helped myself:
How to calculate a minimumScaleFactor
? Divide your label's minimum font size by your label's default font size. For example, your default font size is 25. Your minimum font size is 10.
10/25 = 0.4
0.4 is your minimumScaleFactor
value. Also see @Jsdodgers's answer above.