this post is featured

iOS开发自动计算tabview中label的高度

1
冰之依韩版女装(程序媛福利)

自动计算tabview中label的高度的方法

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(tableView == kindTableView)
    {
        NSInteger section = indexPath.section;

        customCell *cell = (customCell *)[tableView dequeueReusableCellWithIdentifier:customCellView];
        if (cell == nil) {
        [[NSBundle mainBundle] loadNibNamed:@"customCell" owner:self options:nil];
        cell = _customCell;
    }

        FileListItem * item = [cuArray objectAtIndex:indexPath.row];
        cell.textLabel.text = item.name;

        //主要是这里上面的按平常各自的习惯写
        cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
        cell.textLabel.numberOfLines = 0;
        cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:17.0];
        UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:17.0];
        CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT);
        CGSize labelSize = [cell.textLabel.text sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:NSLineBreakByWordWrapping];

        CGRect frame = cell.textLabel.frame;
        cell.textLabel.frame = CGRectMake(frame.origin.x, ((labelSize.height + 20)-frame.size.height)/2, frame.size.width, frame.size.height);

    }
    return cell;

}


//设置cell每行间隔的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *cellText = item.name;
    UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:17.0];
    CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT);
    CGSize labelSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:NSLineBreakByWordWrapping];

    return labelSize.height + 20;
} 


博主app上线了,赶紧点此来围观吧😄


分享文章