如何以减少的字体大小制作文本 UILabel

如果一个 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];
已邀请:

小明明

赞同来自:

descriptionLabel.adjustsFontSizeToFitWidth = YES;
descriptionLabel.minimumFontSize = 10.0; //adjust to preference obviously


在模拟器上测试并检查以下示例。 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";

詹大官人

赞同来自:

在多行中调整文本大小 UILabel, 您可以使用此辅助方法。 /基于
http://www.11pixel.com/blog/28 ... hone/
的 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;
}

小明明

赞同来自:

安装
http://developer.apple.com/iph ... 3-SW8
adjustsFontSizeToFitWidth

价值 YES.

龙天

赞同来自:

如果需要在必要时增加字符串的数量,请使用史蒂夫N, if statement 因此:


if/labelSize.height <= aLabel.frame.size.height/
{
aLabel.numberOfLines = labelSize.height / font.lineHeight;

break;
}

要回复问题请先登录注册