码迷,mamicode.com
首页 > 其他好文 > 详细

sizeThatFits and sizeToFit

时间:2017-07-08 10:01:40      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:data   log   data-   load   rac   name   tput   uila   objc   

http://liuxing8807.blog.163.com/blog/static/9703530520134381526554/


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];
}


sizeThatFits and sizeToFit

标签:data   log   data-   load   rac   name   tput   uila   objc   

原文地址:http://www.cnblogs.com/cynchanpin/p/7135808.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!