我已经设置了一个标签,标签中有一些文字。现在我想设置所有文本都适合标签的字体。我使用了最小字体大小,因为标签文本每天都在变化。但是突然就不行了。我做错了什么?
这是我的代码:
CGRect infoLabelRect = CGRectMake(10, 250, 260, 350);
UILabel *infoLabel = [[UILabel alloc] initWithFrame:infoLabelRect];
infoLabel.text = @"bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla";
infoLabel.numberOfLines = 9;
infoLabel.font = [UIFont systemFontOfSize:17];
infoLabel.minimumFontSize = 12;
infoLabel.adjustsFontSizeToFitWidth = YES;
infoLabel.textColor = [UIColor whiteColor];
infoLabel.textAlignment = NSTextAlignmentCenter;
infoLabel.backgroundColor = [UIColor clearColor];
infoLabel.shadowColor = [UIColor blackColor];
infoLabel.shadowOffset = CGSizeMake(0, 1);
[infoLabel sizeToFit];
我尝试了 rmaddy sad 但没有显示文字。
这是我的代码不起作用。有什么建议么?
UILabel *infoLabel = [[UILabel alloc] initWithFrame:infoLabelRect];
NSString *text = @"bla bla bla";
UIFont *font = nil;
CGFloat points = 17;
CGFloat maxHeight = infoLabel.frame.size.height;
CGFloat textHeight;
do {
font = [UIFont systemFontOfSize:points];
CGSize size = CGSizeMake(infoLabelRect.size.width, 100000);
CGSize textSize = [text sizeWithFont:font constrainedToSize:size lineBreakMode: NSLineBreakByWordWrapping];
textHeight = textSize.height;
points -= 1;
} while (textHeight > maxHeight);
infoLabel.font = font;
infoLabel.textColor = [UIColor whiteColor];
infoLabel.textAlignment = NSTextAlignmentCenter;
infoLabel.backgroundColor = [UIColor clearColor];
infoLabel.shadowColor = [UIColor blackColor];
infoLabel.shadowOffset = CGSizeMake(0, 1);
[infoLabel sizeToFit];
[contentView addSubview:infoLabel];