为什么这会破坏 UILabel adjustsFontSizeToFitWidth? [英] Why does this break UILabel adjustsFontSizeToFitWidth?

查看:151
本文介绍了为什么这会破坏 UILabel adjustsFontSizeToFitWidth?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

iOS 7、Xcode 5

iOS 7, Xcode 5

使用 UILabel,此代码有效(自动调整文本以适应):

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

但是添加setFont"行会导致它无法缩放字体以适合:

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?

推荐答案

好吧,看来这基本上是一个错误,也许在 Xcode 6 中已修复.这适用于 iOS 7 - 我没有在任何其他版本中测试过.

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.

与此同时,这是我用来缩小文本以适应我的 UILabel 的方法,多行和 AttributedString:

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

这篇关于为什么这会破坏 UILabel adjustsFontSizeToFitWidth?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆