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的更多相关文章

  1. iOS--碎片知识锦集

    知识锦集day01 1.UIView的两个方法: sizeThatFits和 sizeToFit 官方文档上说: - (CGSize)sizeThatFits:(CGSize)size;     // ...

  2. 深入理解Auto Layout 第一弹

    本文转载至 http://zhangbuhuai.com/2015/07/16/beginning-auto-layout-part-1/ By 张不坏 2015-07-16 更新日期:2015-07 ...

  3. iOS - 布局重绘机制相关方法的研究

    iOS View布局重绘机制相关方法 布局 - (void)layoutSubviews - (void)layoutIfNeeded- (void)setNeedsLayout —————————— ...

  4. UIButton的探秘

    原文链接 sizeToFit()和sizeThatFits(_:) sizeToFit()会调用sizeThatFits(_:)方法,将现在的frame作为参数.然后根据函数返回的结果更新view. ...

  5. ios UIView sizeToFit sizeThatFits

    UILabel *testLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 50, 0, 0)]; testLabel.backgroundC ...

  6. iOS技术篇:sizeToFit 和 sizeThatFits 区别

    sizeToFit:会计算出最优的 size 而且会改变自己的size UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(, , , ...

  7. 转:UIView的sizeToFit与sizeThatFits

    UILabel经常用到的方法- (void)sizeToFit- (CGSize)sizeThatFits:(CGSize)size解释如下: sizeToFit会自动调用sizeThatFits方法 ...

  8. sizeToFit & sizeThatFits

    [sizeToFit & sizeThatFits] 1.sizeToFit,根据sizeThatFits方法返回的大小来调整receiver的大小.自定义子类不应该覆盖这个方法. 2.siz ...

  9. 重写UILabler的sizeThatFits方法,需要触发两次才会有效果

    #import "ViewController.h" @interface SpecialLabel:UILabel @end @implementation SpecialLab ...

随机推荐

  1. servlet下的request&&response

    request的方法     *获取请求方式: request.getMethod();     * 获取ip地址的方法 request.getRemoteAddr();     * 获得用户清气的路 ...

  2. phpstorm设置代码片段

    tab 向后推进 shift+tab 向前推进 ctrl+d 复制整行 ctrl+Y删除整行 代码片段就是代码快捷键,如果你设置了www.baidu.com这些内容,但是不想一直重复的打,可以设置个代 ...

  3. Android常用依赖库搜集

    图片处理 CircleImageView Git地址:https://github.com/hdodenhof/CircleImageView 图片依赖库 glide Git地址:https://gi ...

  4. 联想 S5 Pro(L78041)免解锁BL 免rec 保留数据 ROOT Magisk Xposed 救砖 ZUI 5.0.123

    >>>重点介绍<<< 第一:本刷机包可卡刷可线刷,刷机包比较大的原因是采用同时兼容卡刷和线刷的格式,所以比较大第二:[卡刷方法]卡刷不要解压刷机包,直接传入手机后用 ...

  5. Microsoft SQL Server学习(五)--操作符聚合函数

    算术运算符 逻辑运算符 比较运算符 聚合函数 算术运算符(+ - * / ) select score*2 as 成绩翻倍 from class_A update class_A set score= ...

  6. thinkphp配置设置

    thinkphp惯例文件是不可更改的,有配置设置通常在Application->Commen->Conf->config.php更改. 绑定数据库信息 //'配置项'=>'配置 ...

  7. 3星|《投机教父尼德霍夫的股票投机术》:2003年的书了。作者97年投机大亏后在CNBC《金钱》栏目上的股市评论文章集。

    查资料作者在97年金融危机中大亏,之后在CNBC<金钱>栏目上跟人合写股市评论文章,本书是那些股评文章的集合.有资料说作者在08年有一次大亏. 从这些文章看,作者是比较冷静地看待股市的,不 ...

  8. pyrthon 简单爬虫实现

    简单爬虫的通用步骤 BY ZKEEER 2017-09-03 2 COMMENTS   本文首发:ZKeeer’s Blog——简单爬虫的通用步骤代码基于 python3.5多图预警,长文预警 知识点 ...

  9. Tomcat环境的搭建

    一.Tomcat的简单介绍 大家应该知道平时所说的C/S和B/S系统架构:C/S架构是基于客户端C和服务端S的,B/S架构是基于浏览器B和S服务端的,B/S架构中的server就是web服务器. To ...

  10. VS2010编译错误:fatal error C1189: #error : This file requires _WIN32_WINNT to be #defined at least to 0x

    下面是彻底解决方法:在工程的stdafx.h中添加(如有类似语句,需注释掉)#ifndef WINVER // Allow use of features specific to Windows 95 ...