Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does this break UILabel adjustsFontSizeToFitWidth?

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

enter image description here

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

enter image description here

Does anyone know a fix for this problem?

like image 386
wayneh Avatar asked Jul 08 '14 16:07

wayneh


3 Answers

UILabel will adjust font size for multiline text if you set;

  1. numberOfLines to non-zero value.
  2. 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;

  • Xcode 7.1.1
  • iPhone 6 device with iOS 9.0.1
  • iPhone 4S simulator with iOS 8.4
  • iPhone 4S simulator with iOS 8.1
like image 65
eonil Avatar answered Nov 19 '22 04:11

eonil


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

like image 33
Omid S. Avatar answered Nov 19 '22 06:11

Omid S.


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;
        }
    }
}
like image 2
wayneh Avatar answered Nov 19 '22 06:11

wayneh