sizeThatFits and sizeToFit
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的更多相关文章
- iOS--碎片知识锦集
知识锦集day01 1.UIView的两个方法: sizeThatFits和 sizeToFit 官方文档上说: - (CGSize)sizeThatFits:(CGSize)size; // ...
- 深入理解Auto Layout 第一弹
本文转载至 http://zhangbuhuai.com/2015/07/16/beginning-auto-layout-part-1/ By 张不坏 2015-07-16 更新日期:2015-07 ...
- iOS - 布局重绘机制相关方法的研究
iOS View布局重绘机制相关方法 布局 - (void)layoutSubviews - (void)layoutIfNeeded- (void)setNeedsLayout —————————— ...
- UIButton的探秘
原文链接 sizeToFit()和sizeThatFits(_:) sizeToFit()会调用sizeThatFits(_:)方法,将现在的frame作为参数.然后根据函数返回的结果更新view. ...
- ios UIView sizeToFit sizeThatFits
UILabel *testLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 50, 0, 0)]; testLabel.backgroundC ...
- iOS技术篇:sizeToFit 和 sizeThatFits 区别
sizeToFit:会计算出最优的 size 而且会改变自己的size UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(, , , ...
- 转:UIView的sizeToFit与sizeThatFits
UILabel经常用到的方法- (void)sizeToFit- (CGSize)sizeThatFits:(CGSize)size解释如下: sizeToFit会自动调用sizeThatFits方法 ...
- sizeToFit & sizeThatFits
[sizeToFit & sizeThatFits] 1.sizeToFit,根据sizeThatFits方法返回的大小来调整receiver的大小.自定义子类不应该覆盖这个方法. 2.siz ...
- 重写UILabler的sizeThatFits方法,需要触发两次才会有效果
#import "ViewController.h" @interface SpecialLabel:UILabel @end @implementation SpecialLab ...
随机推荐
- Spring.Net学习笔记(八)-设置配置文件参数
一.开发环境 VS2013 .netframework4.5 spring.net1.3.1 二.项目结构 三.开发过程 1.编写Person类 namespace SpringNetConfigAr ...
- 【转】rpm包和源码包安装的区别
转自:https://blog.csdn.net/junjie_6/article/details/59483785 建议在安装线上的生产服务器软件包时都用源码安装,这是因为源码安装可以自行调整编译参 ...
- [ Luogu 3935 ] Calculating
\(\\\) \(Description\) 若\(x\)分解质因数结果为\(x=p_1^{k_1}p_2^{k_2}\cdots p_n^{k_n}\),令\(f(x)=(k_1+1)(k_2+1) ...
- Codeforces_768_B_(二分)
B. Code For 1 time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- 导出功能在数据库内容为数字,excel表格中是汉字的时候
代码如下: @ExcelField(title = "饮水器评价",dictType = "waterer_rate" ,align = 2, sort = 2 ...
- 【原】Mysql存储关联数组
$fruits= array("apple" => "苹果", "banana" => "香蕉"," ...
- 08css、JS
08.css.JS-2018/07/18 1.css的属性 文字属性:font-size:大小,font-family字体类型,font-color:颜色 文本颜色:color:颜色,test-dec ...
- 洛谷 2471 BZOJ 1067 [SCOI2007]降雨量
[题解] 用线段树维护区间最大值(因为没有修改,St表也可以),然后由于x,y可能是降雨量未知的年份,需要进行分类讨论. #include<cstdio> #include<algo ...
- 【模板】Lca倍增法
Codevs 1036 商务旅行 #include<cstdio> #include<cmath> #include<algorithm> using namesp ...
- [bzoj4300][绝世好题] (动规)
Description 给定一个长度为n的数列ai,求ai的子序列bi的最长长度,满足bi&bi-1!=0(2<=i<=len). Input 输入文件共2行. 第一行包括一个整数 ...