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来调整高度?
CGFloat titleLabelHeight = [self.titleLabel.text boundingRectWithSize:CGSizeMake(mainScreenWidth - 30, MAXFLOAT)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:@{
NSFontAttributeName : [UIFont systemFontOfSize:16]
} context:nil].size.height;
这个方法计算
size 传入你固定的宽度,高度传个最高
attr 传入你字体的大小
然后会返回一个 CGRect 给你
你自己去拿里面的 height 就可以了
再做不等高 cell 嘛?
- (CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)options attributes:(nullable NSDictionary<NSString *, id> *)attributes context:(nullable NSStringDrawingContext *)context;
用自动布局的话高度不用设定,lable自动会计算高度。
要是没有layout的话,那就是自己计算高度了,label有个 sizeTofit 函数,返回的就是 size,你可以试一下。计算出来高度之后在给lable赋值高度就好了。
UILabel.numberOfLines = 0;
UILabel .lineBreakMode = NSLineBreakByCharWrapping;
[UILabel sizeToFit];
用AutoLayout,可以自动计算高度