UILabel如何使用sizeToFit正确适配行数
来源:互联网 发布:怎么用数据库做购物车 编辑:程序博客网 时间:2024/06/05 03:56
sizeToFit方法,是UIView的一个系统方法,苹果文档如下描述:
- (void)sizeToFit
Description
Resizes and moves the receiver view so it just encloses its subviews.
Call this method when you want to resize the current view so that it uses the most appropriate amount of space. Specific UIKit views resize themselves according to their own internal needs. In some cases, if a view does not have a superview, it may size itself to the screen bounds. Thus, if you want a given view to size itself to its parent view, you should add it to the parent view before calling this method.
You should not override this method. If you want to change the default sizing information for your view, override the sizeThatFits: instead. That method performs any needed calculations and returns them to this method, which then makes the change.
Availability
iOS (2.0 and later)
这个方法,会自动调整view的bounds,使用最小的、符合需求的区域大小。
猜想这个方法用来计算label的宽高会很方便,但是一开始未达到效果,代码如下
UIView *view1 = [[UIView alloc]init];
view1.frame =CGRectMake(100,100,200, 200);
view1.backgroundColor = [UIColor grayColor];
[self.view addSubview:view1];
UILabel *label = [[UILabel alloc]init];
label.frame =CGRectMake(0,0,200,200);
label.backgroundColor = [UIColor redColor];
[view1 addSubview:label];
label.text =@"1dsafafasfasfasfafasfdsafasdfsadfsafdsafasfdsf";
[label sizeToFit];
效果如下:
这个label为什么没有根据view1的宽度来适配呢?原来label的numberOfLines默认为1,此时sizeToFit不改变label的行数。
增加一行代码:
label.numberOfLines = 0;//设置为0,sizeToFit会自动调整行数
效果如下:
- UILabel如何使用sizeToFit正确适配行数
- UILabel如何使用sizeToFit进行宽高适配
- UILabel如何使用sizeToFit进行宽高适配 和 sizeThatFits 以及实现总是顶端显示text的需求
- iOS UIlabel sizeToFit失效的问题
- UILabel文字大小自适应之--SizeToFit
- UILabel文字大小自适应之--SizeToFit
- UILabel文字大小自适应之--SizeToFit
- sizeToFit()使用心得
- 如何教大家正确统计表的行数
- mysql中的存储过程使用事物后如何正确得到影响行数
- sizeToFit
- uilabel设置固定行数
- UILabel行数间隔
- iOS: UILabel的adjustsFontSizeToFitWidth与 sizeToFit与sizeThatFits
- 如何正确使用电烙铁
- 如何正确使用ads
- 如何正确使用异常
- 如何正确使用Timer
- 完成按照日期排列
- matlab 精通指南(例程)
- System.getProperty
- Java之美[从菜鸟到高手演练]之Hadoop原理及架构
- RedisClient
- UILabel如何使用sizeToFit正确适配行数
- flow control通常的方法
- 用adb向手机发送按键事件
- linux strace命令详解 进程跟踪
- Mac Yosemite系统下JAVA_HOME环境变量如何配置
- 国内银行核心系统建设情况调研报告
- 并行开发
- 51瞎搞系列---独立键盘+数码管
- codeforces 259 div2 virtual Participation