- (void)setupLabel {
//准备工作
UILabel *textLabel = [[UILabel alloc] init];
textLabel.font = [UIFont systemFontOfSize:16];
NSString *str = @"222222222222222222222222222222222222222222";
textLabel.text = str;
textLabel.backgroundColor = [UIColor redColor];
textLabel.numberOfLines = 0;//根据最大行数需求来设置
textLabel.lineBreakMode = NSLineBreakByTruncatingTail;
CGSize maximumLabelSize = CGSizeMake(100, 9999);//labelsize的最大值
//关键语句
CGSize expectSize = [textLabel sizeThatFits:maximumLabelSize];
//别忘了把frame给回label,如果用xib加了约束的话可以只改一个约束的值
textLabel.frame = CGRectMake(20, 70, expectSize.width, expectSize.height);
[self.view addSubview:textLabel];
}
Label根据现实内容自适应大小
最新推荐文章于 2021-05-31 06:41:24 发布