1.先cell自适应
可以先拿到 这个lable里文字的高度
//lable自适应的高度
-(CGFloat)heightWithString:(NSString *)aString
{ CGRect r = [aString boundingRectWithSize:CGSizeMake(self.tableView.frame.size.width - 28, 20000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:FourT3GongNengZiHaoFont} context:nil]; return r.size.height;
}
在cell的高度返回方法里面调用方法
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return [ActiviteDetail2TwoCell cellHeight:[self heightWithString:_activityInfoNewModel.activityDes]]; }
在cell里填写此cell需要的高度
+(CGFloat)cellHeight:(CGFloat)height
{ return 72+height;
}
此时cell可以根据文字的高度 而适应高度了
那么cell里的lable的高度怎么设置??
正常在初始化里写正常的高度就可以
self.detailLable.frame = CGRectMake(14, CGRectGetMaxY(self.titleLable.frame)+28+1, CurrentScreenWidth-28, 23);
在cell里写一个改变这个detail的方法
//赋值 and 自动换行,计算出cell的高度
-(void)setDetailLableText:(NSString *)text
{
//获得当前cell高度
CGRect frame = [self frame];
//文本赋值
self.detailLable.text = text;
//设置label的最大行数
self.detailLable.numberOfLines = 10;
CGSize size = CGSizeMake(CurrentScreenWidth-14-30-14, 1000);
CGSize labelSize = [self.detailLable.text sizeWithFont:self.detailLable.font constrainedToSize:size lineBreakMode:NSLineBreakByClipping];
self.detailLable.frame = CGRectMake(self.detailLable.frame.origin.x, self.detailLable.frame.origin.y, labelSize.width, labelSize.height); //计算出自适应的高度
frame.size.height = labelSize.height; self.frame = frame;
}
出来再cell的赋值里面调用这个方法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{ ActiviteDetail2TwoCell *cellTwo = [tableView dequeueReusableCellWithIdentifier:@"ActiviteDetail2TwoCell" forIndexPath:indexPath];
cellTwo.selectionStyle = UITableViewCellSelectionStyleNone; [cellTwo setDetailLableText:_activityInfoNewModel.activityDes]; }
lable的行间距设置
if (cellTwo.detailLable.text!=nil) { //这里要判断一下 改label不为空的时候 不判空 会蹦掉
NSMutableAttributedString * attributedString1 = [[NSMutableAttributedString alloc] initWithString:cellTwo.detailLable.text];
NSMutableParagraphStyle * paragraphStyle1 = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle1 setLineSpacing:3];
[attributedString1 addAttribute:NSParagraphStyleAttributeName value:paragraphStyle1 range:NSMakeRange(0, [cellTwo.detailLable.text length])];
[cellTwo.detailLable setAttributedText:attributedString1];
[cellTwo.detailLable sizeToFit];
}
lable 根据宽度适应字体
titleLabel.adjustsFontSizeToFitWidth = YES;