How does a UILabel's minimumScaleFactor work?

IosObjective CUilabel

Ios Problem Overview


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];

Ios Solutions


Solution 1 - Ios

You need to set the label.adjustsFontSizeToFitWidth = YES;

Solution 2 - Ios

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 - Ios

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 minimumScaleFactorand it should do the same thing as minimumFontSize did.

Solution 4 - Ios

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPadin215View Question on Stackoverflow
Solution 1 - IosScottView Answer on Stackoverflow
Solution 2 - IosJsdodgersView Answer on Stackoverflow
Solution 3 - IoskovpasView Answer on Stackoverflow
Solution 4 - IosAVogView Answer on Stackoverflow