计算高度
//计算高度(此方法需要在.h中声明)
+ (CGFloat)calculateHeightWith:(Student *)student{
CGFloat staticHeight = kImgHeight + 3 * kGap;
//staticHeight是固定高度
CGFloat dynamicHeight = [self hobbyLabelHeight:student];
//dynamicHeight是通过自适应Label的高度获取过来的值
return staticHeight + dynamicHeight; //返回的是cell自适应的高度
}
计算textFiled高度
//计算hobby的高度
+ (CGFloat)hobbyLabelHeight:(Student *)student{
CGRect rect = [student.hobby boundingRectWithSize:CGSizeMake(文本框的宽度, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttribu teName:[UIFont systemFontOfSize:17]} context:nil]; //options 选择中间带Line的一个
//@{ }是一个字典,里面放的是label中字体的大小
return rect.size.height;
}
重新调整label的frame
//使用计算出的结果重新调整hobbyLabel的frame
CGRect hobbyFrame = self.hobbyLabel.frame;
hobbyFrame.siz e.height = [[self class] hobbyLabelHeight:student]; //因为定义的是类方法,所以上方用的self class调用的hobbyLabelHeight
//hobbyFrame.size.height = [CustomCell hobbyLabelHeight:student];//与上方一样
self.hobbyLabel.frame = hobbyFrame;