iOS 7,Xcode 5
使用UILabel,此代码有效(自动调整文本以适应):
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];
Run Code Online (Sandbox Code Playgroud)

但添加"setFont"行会导致它无法缩放字体以适应:
[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];
Run Code Online (Sandbox Code Playgroud)

有谁知道解决这个问题?
Eon*_*nil 19
UILabel 如果设置,将调整多行文字的字体大小;
numberOfLines 到非零值.lineBreakMode到NSLineBreakMode.ByTruncatingTail.例如,
_label.numberOfLines = 2
_label.lineBreakMode = NSLineBreakMode.ByTruncatingTail
_label.adjustsFontSizeToFitWidth = true
_label.minimumScaleFactor = 0.4
Run Code Online (Sandbox Code Playgroud)
经过测试并确认可以使用;
好吧,看起来这基本上是一个错误,也许在 Xcode 6 中已修复。这在 iOS 7 中有效 - 我还没有在任何其他版本中测试过它。
同时,这是我用来缩小文本以适合我的 UILabel 的方法,具有多行和 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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11717 次 |
| 最近记录: |