sizeThatFits and sizeToFit是UIView的两个方法
2016-05-29 17:21
459 查看
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];
}
- (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];
}
相关文章推荐
- 【Android UI】TextView的垂直方向概念之top,bottom,ascent,descent,baseline
- 在web应用中使用 logback, log4j 打印request中的相关信息
- Duilib学习总结
- UIKit框架之——UILabel
- 2.5 UI组件-AdapterView及子类(疯狂android学习笔记)
- QuickHit游戏
- 05章项目: QuickHit快速击键
- 算法思想系列--分治策略(Divide-Conquer-Combine)(待补充)
- SAPUI5 学习笔记(1)
- 1976 Queen数列
- android与arduino手机控制项目开源
- 浅谈arduino与android通信
- arduino 大小端数据问题
- Introduction of Algorithm - Divide and Conquer(分治法)
- SGU-181 X-Sequence
- iOS AutoLayout: 关联 Xib 和 UIViewController
- X-Sequence
- 【Android UI】图片 + 文字展示by SpannableStringBuilder
- iOS开发中的UDID和UUID详解
- hadoop key和value 分隔符号设置