iOS UILabel根据字符串长度自动适应宽度和高度
2017-01-17 本文已影响273人
子斌
UILabel *label = [[UILabel alloc] init];
label.text = @"这是用于测试的文字"
label.numberOfLines = 0;
NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:20],};
CGSize textSize = [label.text boundingRectWithSize:CGSizeMake(100, 100) options:NSStringDrawingTruncatesLastVisibleLine attributes:attributes context:nil].size;;
[label setFrame:CGRectMake(100, 100, textSize.width, textSize.height)];