// 这个frame是无所谓的,后面还会重新设置它的size。
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0,0,0,0)];
label.numberOfLines = 0;
// 创建一个字体大小的字典
NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:20],};
NSString *str = @"自适应长度";
// 第一个参数填可达到最大的长度宽度 一般填很大
CGSize newSize = [str boundingRectWithSize:CGSizeMake(100000, 10000) options:NSStringDrawingTruncatesLastVisibleLine attributes:attributes context:nil].size;;
[label setFrame:CGRectMake(100, 100, newSize.width, newSize.height)];
UILabel的自适应长度
最新推荐文章于 2021-06-24 11:39:08 发布
