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;

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容