这是我的代码
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 50, 300, 50)];
label.textAlignment = UITextAlignmentCenter;
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor whiteColor];
label.textColor.font = [UIFont fontWithName:@"Verdana" size:30];
label.text = @"A very long string";
etc...
问题是字体太大装不下标签,只显示"A very"。
应该怎么做才能让整个文本都显示出来呢? 我已经尝试过
label.lineBreakMode = UILineBreakModeWordWrap;
label.numberOfLines = 0;
但是这对我没有用。
我希望能够以编程的方式实现这个功能。
//编辑
CGRect frame = CGRectMake(10, 50, 300, 50);
NSString *labelString = @"Players.";
UILabel *howManyUsersLabel = [[UILabel alloc]initWithFrame:frame];
howManyUsersLabel.textAlignment = UITextAlignmentCenter;
howManyUsersLabel.backgroundColor = [UIColor clearColor];
howManyUsersLabel.textColor = [UIColor whiteColor];
howManyUsersLabel.adjustsFontSizeToFitWidth = NO;
howManyUsersLabel.numberOfLines = 0;
CGFloat fontSize = 30;
while (fontSize > 0.0)
{
CGSize size = [labelString sizeWithFont:[UIFont fontWithName:@"Verdana" size:fontSize] constrainedToSize:CGSizeMake(frame.size.width, 10000) lineBreakMode:UILineBreakModeWordWrap];
if (size.height <= frame.size.height) break;
fontSize -= 1.0;
NSLog(@"test");
}
howManyUsersLabel.font = [UIFont fontWithName:@"Verdana" size:fontSize];