sizeThatFits 和 sizeToFit区别

- (void)viewDidLoad
{
    [super viewDidLoad];
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 200, 100)];
    view.backgroundColor = [UIColor yellowColor];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, 0, 0)];
    [label setFont:[UIFont systemFontOfSize:20]];
    label.text = @"hello wdszgrf";
    CGSize sizeThatFits = [label sizeThatFits:CGSizeZero];
    NSLog(@"---- %f  %f ----", sizeThatFits.width, sizeThatFits.height);
    // output:  ---- 117.000000  24.000000 ----
    
    NSLog(@"**** %f  %f ****", label.frame.size.width, label.frame.size.height);
    // output:  **** 0.000000  0.000000 **** 说明sizeThatSize并没有改变原始label的大小
    
    [label sizeToFit];  // 这样搞就直接改变了这个label的宽和高,使它根据上面字符串的大小做合适的改变
    [label setCenter:CGPointMake(80, 50)];
    NSLog(@"==== %f %f ====", label.frame.size.width, label.frame.size.height);
    // output:   ==== 117.000000 24.000000 ====
    
    [view addSubview:label];
    [self.view addSubview:view];
}
sizeThatFits不会改变控件原始尺寸
sizeToFit会改变控件原始尺寸

你可能感兴趣的:(iOS,sizeThatFits,sizeToFit,uilabel,自适应)