If a UILabel contains too much text, how can I setup my label so that it shrinks font-sizes?
如果UILabel包含太多文本,我如何设置我的标签以缩小字体大小?
Here is how I am setting up my UILabel:
以下是我设置UILabel的方法:
descriptionLabel = [[UILabel alloc] initWithFrame:CGRectMake(200, 30, 130, 150)];
[descriptionLabel setFont:[Utils getSystemFontWithSize:14]];
[descriptionLabel setBackgroundColor:[UIColor clearColor]];
[descriptionLabel setTextColor:[UIColor whiteColor]];
descriptionLabel.numberOfLines = 1;
[self addSubview:descriptionLabel];
4 个解决方案
#1
59
descriptionLabel.adjustsFontSizeToFitWidth = YES;
descriptionLabel.minimumFontSize = 10.0; //adjust to preference obviously
The following example is tested and verified on iPhone Simulator 3.1.2:
以下示例在iPhone模拟器3.1.2上进行了测试和验证:
UILabel *descriptionLabel = [[UILabel alloc] initWithFrame:CGRectMake(90, 0, 200, 30)];
descriptionLabel.font = [UIFont systemFontOfSize:14.0];
descriptionLabel.minimumFontSize = 10.0;
descriptionLabel.adjustsFontSizeToFitWidth = YES;
descriptionLabel.numberOfLines = 1;
descriptionLabel.text = @"supercalifragilisticexpialidocious even thought he sound of it is something quite attrocious";
#2
21
To resize the text in a multi-line UILabel, you can use this helper method (based on code from 11 Pixel Studios):
要在多行UILabel中调整文本大小,可以使用此辅助方法(基于11 Pixel Studios的代码):
+ (void)resizeFontForLabel:(UILabel*)aLabel maxSize:(int)maxSize minSize:(int)minSize {
// use font from provided label so we don't lose color, style, etc
UIFont *font = aLabel.font;
// start with maxSize and keep reducing until it doesn't clip
for(int i = maxSize; i >= minSize; i--) {
font = [font fontWithSize:i];
CGSize constraintSize = CGSizeMake(aLabel.frame.size.width, MAXFLOAT);
// This step checks how tall the label would be with the desired font.
CGSize labelSize = [aLabel.text sizeWithFont:font constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
if(labelSize.height <= aLabel.frame.size.height)
break;
}
// Set the UILabel's font to the newly adjusted font.
aLabel.font = font;
}
#4
0
If you want the number of lines to also increase if needed, use Steve N's solution, with the if statement as so:
如果您希望在需要时也增加行数,请使用Steve N的解决方案,if语句如下:
if(labelSize.height <= aLabel.frame.size.height)
{
aLabel.numberOfLines = labelSize.height / font.lineHeight;
break;
}
#1
59
descriptionLabel.adjustsFontSizeToFitWidth = YES;
descriptionLabel.minimumFontSize = 10.0; //adjust to preference obviously
The following example is tested and verified on iPhone Simulator 3.1.2:
以下示例在iPhone模拟器3.1.2上进行了测试和验证:
UILabel *descriptionLabel = [[UILabel alloc] initWithFrame:CGRectMake(90, 0, 200, 30)];
descriptionLabel.font = [UIFont systemFontOfSize:14.0];
descriptionLabel.minimumFontSize = 10.0;
descriptionLabel.adjustsFontSizeToFitWidth = YES;
descriptionLabel.numberOfLines = 1;
descriptionLabel.text = @"supercalifragilisticexpialidocious even thought he sound of it is something quite attrocious";
#2
21
To resize the text in a multi-line UILabel, you can use this helper method (based on code from 11 Pixel Studios):
要在多行UILabel中调整文本大小,可以使用此辅助方法(基于11 Pixel Studios的代码):
+ (void)resizeFontForLabel:(UILabel*)aLabel maxSize:(int)maxSize minSize:(int)minSize {
// use font from provided label so we don't lose color, style, etc
UIFont *font = aLabel.font;
// start with maxSize and keep reducing until it doesn't clip
for(int i = maxSize; i >= minSize; i--) {
font = [font fontWithSize:i];
CGSize constraintSize = CGSizeMake(aLabel.frame.size.width, MAXFLOAT);
// This step checks how tall the label would be with the desired font.
CGSize labelSize = [aLabel.text sizeWithFont:font constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
if(labelSize.height <= aLabel.frame.size.height)
break;
}
// Set the UILabel's font to the newly adjusted font.
aLabel.font = font;
}
#3
#4
0
If you want the number of lines to also increase if needed, use Steve N's solution, with the if statement as so:
如果您希望在需要时也增加行数,请使用Steve N的解决方案,if语句如下:
if(labelSize.height <= aLabel.frame.size.height)
{
aLabel.numberOfLines = labelSize.height / font.lineHeight;
break;
}