sizeThatFits and sizeToFit

本文介绍了iOS开发中UILabel的两种尺寸调整方法:sizeThatFits与sizeToFit的区别及应用场景。通过实例展示了单行和多行文本下不同方法的效果,为开发者提供了实用的布局调整指南。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

其两者的区别,通过一个小例子便可看出:

 NSString *str=@"目前支持以下站点";
    UILabel *notice=[[UILabel alloc]initWithFrame:CGRectMake(20, 100, 200, 20)];
    //文本文字自适应大小
    notice.font = [UIFont systemFontOfSize:14];
    notice.text=str;
    notice.textAlignment=NSTextAlignmentCenter;
    //使用sizeThatFit计算lable大小
    CGSize sizeThatFit=[notice sizeThatFits:CGSizeZero];
    //重新指定frame
    NSLog(@"---- %f  %f ----", sizeThatFit.width, sizeThatFit.height);
    NSLog(@"**** %f  %f ****", notice.frame.size.width, notice.frame.size.height);
    NSLog(@"------------------------------------------");

    [notice sizeToFit];
    NSLog(@"**** %f  %f ****", notice.frame.size.width, notice.frame.size.height);
    notice.textColor=[UIColor whiteColor];
    notice.backgroundColor=[UIColor blackColor];
    [self.view addSubview:notice];

以上输出结果:

2016-12-30 10:18:51.866 UI部分[7490:87265] ---- 114.500000  17.000000 ----
2016-12-30 10:18:51.871 UI部分[7490:87265] **** 200.000000  20.000000 ****
2016-12-30 10:18:51.872 UI部分[7490:87265] ------------------------------------------
2016-12-30 10:18:51.872 UI部分[7490:87265] **** 114.500000  17.000000 ****

从以上输出结果可以看出

1.sizeThatFits并没有改变label的frame,只会计算出文本的size
2.sizeToFit会改变这个label的宽和高,使它根据上面字符串的大小做合适的改变

进一步比较:

- (void)sizeToFits2
{
    NSString *str=@"sizeToFits2目前支持以下站点sizeToFits2,sizeToFits2目前支持以下站点sizeToFits2,sizeToFits2目前支持以下站点sizeToFits2";
    UILabel *notice=[[UILabel alloc]initWithFrame:CGRectMake(20, 300, 200, 20)];
    //文本文字自适应大小
    notice.font          = [UIFont systemFontOfSize:14];
    notice.text          =str;
    notice.numberOfLines = 0;
    [notice sizeToFit];
    notice.textColor     =[UIColor whiteColor];
    notice.backgroundColor=[UIColor blackColor];
    [self.view addSubview:notice];

}


- (void)sizeThatFits2
{
    NSString *str=@"sizeThatFits2目前支持以下站点sizeThatFits2.sizeThatFits2目前支持以下站点sizeThatFits2,sizeThatFits2目前支持以下站点sizeThatFits2";
    UILabel *notice=[[UILabel alloc]initWithFrame:CGRectMake(20, 100, 200, 20)];
    //文本文字自适应大小
    notice.font          = [UIFont systemFontOfSize:14];
    notice.text          =str;
    notice.numberOfLines = 0;
    CGSize sizeThatFit=[notice sizeThatFits:CGSizeZero];
    notice.frame         = CGRectMake(notice.frame.origin.x, notice.frame.origin.y, sizeThatFit.width, sizeThatFit.height);
    notice.textColor     =[UIColor whiteColor];
    notice.backgroundColor=[UIColor blackColor];
    [self.view addSubview:notice];

}

这里写图片描述

对比:
1、当不设置多行时,两者并没有什么差别。
2、当文字较多时,设置numberOfLines = 0,效果如上图所示,sizeThatFits并不会折行显示,sizeToFits会在设置的宽度内这行显示。

因此,处理但行文本操作时两者都可以,多行时使用sizeToFits可以完成。

(Dialog BamTo_Template (Components (SubLayout Layout1) (SubLayout Layout2) ) (Resources (.Label "设置") (.StartLocation 5) (.ResourceHints "Version:Creo4") (.Resizeable False) (.SizeToFit True) (.CanReduceWidth True) (.CanReduceHeight True) (.Layout (Grid (Rows 0 0) (Cols 0) Layout1 Layout2 ) ) ) ) (Layout Layout1 (Components (Label Label1) (InputPanel InputPanel1) (PushButton PushButton1) (Label Label2) (InputPanel InputPanel2) (PushButton PushButton2) (Label Label3) (InputPanel InputPanel3) (PushButton PushButton3) ) (Resources (Label1.Label "零件绘图模版") (Label1.AttachLeft True) (InputPanel1.Columns 30) (InputPanel1.MaxLen 100) (InputPanel1.MinColumns 0) (InputPanel1.HelpTextAlignment 1) (PushButton1.Label "选择") (PushButton1.Columns 5) (Label2.Label "钣金绘图模版") (Label2.AttachLeft True) (InputPanel2.MaxLen 100) (PushButton2.Label "选择") (Label3.Label "组件绘图模版") (Label3.AttachLeft True) (InputPanel3.MaxLen 100) (PushButton3.Label "选择") (.Label "绘图模版设置") (.Columns 50) (.Decorated 1) (.AttachLeft True) (.AttachRight True) (.AttachTop True) (.AttachBottom True) (.TopOffset 3) (.BottomOffset 3) (.LeftOffset 3) (.RightOffset 3) (.CanReduceWidth True) (.CanReduceHeight True) (.Layout (Grid (Rows 1 0 0) (Cols 0 1 0) Label1 InputPanel1 PushButton1 Label2 InputPanel2 PushButton2 Label3 InputPanel3 PushButton3 ) ) ) ) (Layout Layout2 (Components (CheckButton CheckButton1) (CheckButton CheckButton2) (CheckButton CheckButton3) (CheckButton CheckButton4) (CheckButton CheckButton5) ) (Resources (CheckButton1.Label "导出STP") (CheckButton2.Label "导出IGS") (CheckButton3.Label "导出DWG") (CheckButton4.Label "导出PDF") (CheckButton5.Label "记录工作目录") (.Label "导出设置") (.Columns 50) (.Decorated 1) (.TopOffset 3) (.BottomOffset 3) (.LeftOffset 3) (.RightOffset 3) (.Layout (Grid (Rows 1) (Cols 1 1 1 1 1) CheckButton1 CheckButton2 CheckButton3 CheckButton4 CheckButton5 ) ) ) ) 这是我的对话框代码,里面有右上角关闭的按钮么
最新发布
08-13
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值