• sizeThatFits and sizeToFit


    sizeThatFits and sizeToFit是UIView的两个方法, 官方文档上说: 
    - (CGSize)sizeThatFits:(CGSize)size;     
    作用:return 'best' size to fit given size. does not actually resize view. Default is return existing view size
    - (void)sizeToFit;     
    作用: calls sizeThatFits: with current view bounds and changes bounds size.
    
    - (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];
    }
    

  • 相关阅读:
    hibernate 4 需要导入的jar包
    中国象棋马走日(要求打印每一种走法) — 递归
    最长公共子串
    国际象棋马走日(骑士周游)
    八皇后
    约瑟夫问题
    hashtable的运用实例
    用较小的代价去除字符串中的空格
    C++ string数据类型的实现
    strcpy、strncpy与memcpy的区别与使用方法
  • 原文地址:https://www.cnblogs.com/zsw-1993/p/4879450.html
Copyright © 2020-2023  润新知