label的width是固定的,输入不同的内容,比如“hello” 或者 “prevents me from using standard methods to resize the UILabel. I read on another post here that I'm supposed to use the function“ 后,label根据label.text来调整高度?
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
这个方法计算
size 传入你固定的宽度,高度传个最高
attr 传入你字体的大小
然后会返回一个 CGRect 给你
你自己去拿里面的 height 就可以了
再做不等高 cell 嘛?
用AutoLayout,可以自动计算高度
UILabel.numberOfLines = 0;
UILabel .lineBreakMode = NSLineBreakByCharWrapping;
[UILabel sizeToFit];
用自动布局的话高度不用设定,lable自动会计算高度。
要是没有layout的话,那就是自己计算高度了,label有个 sizeTofit 函数,返回的就是 size,你可以试一下。计算出来高度之后在给lable赋值高度就好了。