我有一个UITableViewCell
,有时是UILabel
中的文本(UILabel
是UITableViewCell
中的 subview ) )超出了尺寸,并且标签中出现了一些点(例如...)。
我想调整字体大小以适应宽度。 但是它不起作用。
这是我的代码 UITableviewCell 的类实现
-(void) awakeFromNib{
myLabel.minimumFontSize = 8;
myLabel.adjustsFontSizeToFitWidth = YES;
}
请帮助我!!!
最佳答案
尝试这个简单的代码
UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
label.backgroundColor=[UIColor clearColor];
label.text= yourString;
label.numberOfLines=0;
label.adjustsFontSizeToFitWidth=YES;
label.minimumFontSize=6;
label.textAlignment=UITextAlignmentCenter;
[self.View addSubview:label];
CGFloat fontSize = 20;
while (fontSize > 0.0)
{
CGSize size = [yourString sizeWithFont:[UIFont fontWithName:@"Rockwell-Bold" size:fontSize] constrainedToSize:CGSizeMake(label.frame.size.width, 10000) lineBreakMode:UILineBreakModeWordWrap];
if (size.height <= label.frame.size.height) break;
fontSize -= 1.0;
}
//set font size
label.font = [UIFont fontWithName:@"Rockwell-Bold" size:fontSize];
[label release];
关于ios - 调整 UILabel(UITableViewCell 的 subview )中的文本大小以适合宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17140490/