效果
- 1.需要在model里面再加一个高度的属性
@property(nonatomic,assign) float cellHeight;
- 2.在model.m文件里两种操作来控制cell里面文字的大小
- (void)setText:(NSString *)text{ _text = text; 方法一: UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(5, 5, [UIScreen mainScreen].bounds.size.width-10, 0)]; label.text = text; label.font = [UIFont systemFontOfSize:30]; label.numberOfLines = 0; CGSize s = [label sizeThatFits:CGSizeMake([UIScreen mainScreen].bounds.size.width-10, MAXFLOAT)]; self.cellHeight = s.height + 310; 方法二: CGSize size = [text boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width-20, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:30]} context:nil].size; self.cellHeight = size.height + 310; }
- 3.自定义的label在重写UITableViewCell里面进行返回model的高度
-(void)setModel:(ModelCell *)model { _model = model; self.iconImage.image = [UIImage imageNamed:model.icon]; self.Labeltext.font = [UIFont systemFontOfSize:30]; CGRect rect = self.Labeltext.frame; rect.size.height = model.cellHeight-310; self.Labeltext.numberOfLines = 0; self.Labeltext.frame = rect; NSString *string = [NSString stringWithFormat:@" %@",model.text]; self.Labeltext.text = string ; }
需要强调的是:2里面和3里面字体的大小要保持一致,还有宽度
- 4.在UITableViewController 里面cell高度返回里面写入
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { ModelCell *model = dataArray[indexPath.row]; return model.cellHeight; }
5.具体的代码如下github下载