iOS 7, Xcode 5
Using a UILabel, this code works (autosizes the text to fit):
self.testLabel.numberOfLines=0;
self.testLabel.lineBreakMode=NSLineBreakByWordWrapping;
self.testLabel.adjustsFontSizeToFitWidth=YES;
self.testLabel.minimumScaleFactor=0.1;
self.testLabel.textAlignment=NSTextAlignmentCenter;
[self.testLabel.font fontWithSize:100.0];

But adding the "setFont" line causes it to not scale the font to fit:
[self.testLabel setFont:[UIFont fontWithName:@"Helvetica Neue" size:62.0f]]; //THIS LINE CAUSES THE FONT SCALING TO FAIL
self.testLabel.numberOfLines=0;
self.testLabel.lineBreakMode=NSLineBreakByWordWrapping;
self.testLabel.adjustsFontSizeToFitWidth=YES;
self.testLabel.minimumScaleFactor=0.1;
self.testLabel.textAlignment=NSTextAlignmentCenter;
[self.testLabel.font fontWithSize:100.0];

Does anyone know a fix for this problem?
UILabel will adjust font size for multiline text if you set;
numberOfLines to non-zero value.lineBreakMode to NSLineBreakMode.ByTruncatingTail.For example,
    _label.numberOfLines                =   2
    _label.lineBreakMode                =   NSLineBreakMode.ByTruncatingTail
    _label.adjustsFontSizeToFitWidth    =   true
    _label.minimumScaleFactor           =   0.4
Tested and confirmed to work with;
same thing happened to me, I'm using a non standard Font :
         titleLabel.font = [UIFont fontWithName:@"IRANSans" size:27 ];
         titleLabel.numberOfLines = 1;
         titleLabel.minimumScaleFactor = 0.01;
         titleLabel.adjustsFontSizeToFitWidth = YES;
I found the problem to be the size of the UILabel it's self was growing too, I just added a constraint to keep the label's width fixed, then it worked like a charm. p.s. I'm on xcode 7.1 , iOS 9.2.1
Well, it appears that this is basically a bug, perhaps fixed in Xcode 6. This works in iOS 7 - I have not tested it in any other version.
In the meantime, this is the method I'm using to scale down text to fit in my UILabel, with multiple lines and AttributedString:
-(void)whAlertDisplayTheNotice:(UILabel*)inputlabel theNotice:(NSString*)theNotice {
    float maxFontSize=80.0;
    NSRange tmpRange=NSMakeRange(0,theNotice.length);
    NSMutableParagraphStyle *paragraph=[[NSMutableParagraphStyle alloc]init];
    paragraph.lineBreakMode=NSLineBreakByWordWrapping;
    paragraph.alignment=NSTextAlignmentCenter;
    paragraph.maximumLineHeight=maxFontSize;
    paragraph.lineSpacing=0;
    NSMutableAttributedString *attString=[[NSMutableAttributedString alloc] initWithString:theNotice];
    [attString addAttribute:NSParagraphStyleAttributeName
                      value:paragraph
                      range:tmpRange];
    [attString addAttribute:NSForegroundColorAttributeName
                      value:[UIColor blackColor]
                      range:tmpRange];
    CGSize constraintSize=CGSizeMake(inputlabel.frame.size.width, CGFLOAT_MAX);
    CGRect labelSize;
    for(short i=maxFontSize; i>8; i--){
        [attString addAttribute:NSFontAttributeName
                          value:[UIFont fontWithName:@"MV Boli" size:i]
                          range:tmpRange];
        inputlabel.attributedText=attString;
        labelSize=[inputlabel.attributedText boundingRectWithSize:constraintSize
                                                          options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)
                                                          context:nil];
        if(labelSize.size.height<inputlabel.frame.size.height){
            NSLog(@"fontsize is:%li",(long)i);
            break;
        }
    }
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With