UILabel *testLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 50, 0, 0)];
    testLabel.backgroundColor = [UIColor whiteColor];
    testLabel.text = @"我们都有一个家啊,名字叫中国,家里攀着两条龙";
    testLabel.font = [UIFont systemFontOfSize:20];
    testLabel.textColor = [UIColor blackColor];

    [testLabel sizeThatFits:CGSizeMake(20, 20)];//会计算出最优的 size 但是不会改变 自己的 size,个人认为这个就是 label 自适应大小有用别的没什么用
    NSLog(@"testLabel sizeThatFits frame = %@", NSStringFromCGRect(testLabel.frame));
    NSLog(@"best size = %@",NSStringFromCGSize([testLabel sizeThatFits:CGSizeMake(20, 20)]));
    [testLabel sizeToFit];//会计算出最优的 size 而且会改变自己的size
    NSLog(@"testLabel sizeToFit frame = %@",NSStringFromCGRect(testLabel.frame));
[self.view addSubview:testLabel];

 

 

相关文章:

  • 2022-03-08
  • 2021-06-10
  • 2022-12-23
  • 2022-02-07
  • 2022-12-23
  • 2022-12-23
  • 2021-05-25
  • 2022-01-15
猜你喜欢
  • 2022-02-07
  • 2022-12-23
  • 2021-11-17
  • 2021-08-20
  • 2021-05-21
  • 2021-09-17
  • 2022-12-23
相关资源
相似解决方案