谈谈 sizeToFit 与 sizeThatFit
来源:互联网 发布:成都多益网络面试通知 编辑:程序博客网 时间:2024/05/16 17:16
先看看苹果官方文档对这连个的方法的解释:
- (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.
意思是说,sizeThatFits: 会计算出最优的 size 但是不会改变 自己的 size,而 sizeToFit: 会计算出最优的 size 而且会改变自己的 size。那么两者的联系是什么呢?
实际上,当调用 sizeToFit 后会调用 sizeThatFits 方法来计算 UIView 的 bounds.size 然后改变 frame.size。也就是说,其实我们也可以不使用 [ label sizeToFit] 来计算 label 内容的 size ,首先调用 sizeThatFits 方法或者一个 CGSize 然后改变 label.frame.size 就可以得到 [label sizeToFit]一样的效果。下面用实例加以说明:
UILabel *testLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 100, 20)]; testLabel.text = @"欢迎关注黄飞的csdn博客"; testLabel.font = [UIFont systemFontOfSize:14]; testLabel.textAlignment=NSTextAlignmentCenter; //使用sizeThatFit计算lable大小 CGSize sizeThatFit=[testLabel sizeThatFits:CGSizeZero]; NSLog(@"%f-----%f", sizeThatFit.width, sizeThatFit.height); NSLog(@"%f-----%f", testLabel.frame.size.width, testLabel.frame.size.height); // 调用sizeToFit [testLabel sizeToFit]; NSLog(@"%f-----%f", testLabel.frame.size.width, testLabel.frame.size.height); testLabel.textColor=[UIColor blackColor]; testLabel.backgroundColor=[UIColor yellowColor]; [self.view addSubview:testLabel];
运行结果如下所示:
这也验证了官方的对其的解释:sizeThatFits: 会计算出最优的 size 但是不会改变 自己的 size,而 sizeToFit: 会计算出最优的 size 而且会改变自己的 size。
上述说的是 Label 单行显示时的情形,当 Label 文本较长以至于不能单行显示时,两者也是有区别的。
sizeThatFit:
UILabel *testLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 100, 50, 20)]; testLabel.text = @"欢迎关注黄飞的csdn博客,这里有你想要的东西,欢迎关注!!"; testLabel.font = [UIFont systemFontOfSize:14]; testLabel.textAlignment=NSTextAlignmentCenter; testLabel.numberOfLines = 0; //使用sizeThatFit计算lable大小 CGSize sizeThatFit = [testLabel sizeThatFits:CGSizeZero]; testLabel.frame = CGRectMake(testLabel.frame.origin.x, testLabel.frame.origin.y, sizeThatFit.width, sizeThatFit.height); testLabel.textColor=[UIColor blackColor]; testLabel.backgroundColor=[UIColor yellowColor]; [self.view addSubview:testLabel];
sizeToFit:
UILabel *testLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 100, 100, 20)]; testLabel.text = @"欢迎关注黄飞的csdn博客,这里有你想要的东西,欢迎关注!!"; testLabel.font = [UIFont systemFontOfSize:14]; testLabel.textAlignment=NSTextAlignmentCenter; testLabel.numberOfLines = 0; [testLabel sizeToFit]; testLabel.textColor=[UIColor blackColor]; testLabel.backgroundColor=[UIColor yellowColor]; [self.view addSubview:testLabel];
两者的效果对比如下:
当设置多行显示时,两者又体现出区别,sizeThatFits并不会折行显示,sizeToFits会在设置的宽度内这行显示。这实际上又从另一方面验证了官方的解释。
知其然,更要知其所以然,学习没有捷径,坚持和专研是硬道理,多分享则乐趣无穷!欢迎关注后续博文!
阅读全文
1 0
- 谈谈 sizeToFit 与 sizeThatFit
- sizeToFit和sizeThatFit
- sizeToFit和sizeThatFit
- sizeToFit与adjustFontsToFit
- sizeToFit
- 布局之extendedLayout与sizeToFit
- sizeToFit与sizeThatFits的使用和区别
- iOS: UILabel的adjustsFontSizeToFitWidth与 sizeToFit与sizeThatFits
- UILable sizeToFit
- iOS sizeToFit
- ios-sizeToFit
- sizeThatFits and sizeToFit
- sizeThatFits and sizeToFit
- sizeToFit的用法
- sizeToFit自动改变尺寸
- sizeThatFits and sizeToFit
- sizeToFit()使用心得
- sizeThatFits 和 sizeToFit区别
- requests库入门-1-安装requests库
- 面试题总结之windows/linux内存管理
- maven的环境配置及在Eclipse里的配置
- 多核CPU配合负载均衡可以这样用,为老板省点钱
- 补码——为cpu运算产生的补码
- 谈谈 sizeToFit 与 sizeThatFit
- [py]django api测试
- Maven初体验
- uboot移植:Flash分区和DDR分区
- 传输层协议——UDP协议
- 【MySQL】——常用两种数据引擎
- Swift中的选项集合
- 之前写的CMPP3.0未完成代码
- 高斯过程