119

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];
Bartłomiej Semańczyk
  • 59,234
  • 49
  • 233
  • 358
Padin215
  • 7,444
  • 13
  • 65
  • 103

4 Answers4

228

You need to set the label.adjustsFontSizeToFitWidth = YES;

Scott
  • 2,766
  • 1
  • 15
  • 4
  • 1
    Perfect, this is what I was missing in my own code. Thanks :D – Fogmeister Mar 12 '13 at 08:31
  • 18
    Note: "Apple's documentation says the minimumScaleFactor only works if numberOfLines is set to 1. Using 0 or anything greater than 1 will not work." . Thanks SeanK. – samthui7 Jun 03 '15 at 06:35
  • Awesome! Funny that if you set minumumScaleFactor in the XIB you don't need to set the adjustsFontSizeToFitWidth, but you need when doing in the code. – Politta Mar 09 '17 at 20:40
109

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

Jsdodgers
  • 5,253
  • 2
  • 20
  • 36
  • the division should be casted as CGFloat, otherwise it wouldn't work – dwery Dec 15 '14 at 00:35
  • @dwery Doing 10/labelSize may end up with a truncated integer value of 0 but 10.0 or 10.f work fine, without explicitly casting. – Kalle May 13 '15 at 05:58
  • you're right, it should work. I can't remember in which code it didn't work for me, probably it was missing the f or the dot. – dwery May 13 '15 at 10:23
20

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.

kovpas
  • 9,553
  • 6
  • 40
  • 44
  • Thats what i thought also; but even when i put it as 0.1f, the text does not adjust size. i'll add the uilabel code. – Padin215 Feb 01 '13 at 16:54
  • What if you remove constraints and just shrink label's frame? – kovpas Feb 01 '13 at 17:51
  • 10
    Log139: What is your numberOfLines set to? Apple's documentation says the minimumScaleFactor only works if numberOfLines is set to 1. Using 0 or anything greater than 1 will not work. – SeanK Aug 02 '13 at 18:27
  • 1
    @SeanK It will work even if numberOfLines is set to more than 1 but NOT 0. – Asadullah Ali Apr 07 '16 at 07:53
17

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.

AVog
  • 301
  • 3
  • 10