您好,欢迎来到伴沃教育。
搜索
您的当前位置:首页Label Button 文字大小自适应

Label Button 文字大小自适应

来源:伴沃教育

1、确定Label或Button的字体大小,使其宽度自适应

UILabel *contentLabel = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 120, 30)];
contentLabel.font = [UIFont systemFontOfSize:15];//-------->定义Font的大小
contentLabel.backgroundColor = [UIColor redColor];
contentLabel.text = @"我已知晓,且已阅读并同意以上条款";
[contentLabel sizeToFit];//-------->注意和上面一句代码顺序不能颠倒
[self.View addSubview:contentLabel];

2、确定Label或Button的宽度,使字体大小自适应

//无需自己设置字体大小
UILabel *contentLabel = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 120, 30)];
contentLabel.backgroundColor = [UIColor redColor];
contentLabel.text = @"我已知晓,且已阅读并同意以上条款";
contentLabel.adjustsFontSizeToFitWidth = YES;//默认为NO-------->注意和上面一句代码顺序不能颠倒
[self.View addSubview:contentLabel];

如果是Button的话,和上面一样,只有一点小小的区别:

[button.titleLabel sizeToFit];
button.titleLabel.adjustsFontSizeToFitWidth = YES;

Copyright © 2019- bangwoyixia.com 版权所有 湘ICP备2023022004号-2

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务