# Calculating label heights automatically in a table view

Language: en
Canonical: [Calculating label heights automatically in a table view](<https://allluckly.cn/en/journal/2015-05-17-calculating-label-heights-automatically-in-a-table-view/>)
中文：[中文 HTML](<https://allluckly.cn/uitabview/%E8%87%AA%E5%8A%A8%E8%AE%A1%E7%AE%97tabview%E4%B8%ADlabel%E7%9A%84%E9%AB%98%E5%BA%A6>)
English: [English HTML](<https://allluckly.cn/en/journal/2015-05-17-calculating-label-heights-automatically-in-a-table-view/>)
Published: 2015-05-17T00:00:00+08:00
Modified: 2026-10-23T10:34:11+08:00

Calculates the height a label needs from its font, constrained width and measured text, then adds spacing in the UITableView row-height callback. The code illustrates manual sizing of multiline labels and table-view cells.

The original does not clearly identify an author. Retained as a historical article.

English translation of the [Chinese original](<https://allluckly.cn/uitabview/%E8%87%AA%E5%8A%A8%E8%AE%A1%E7%AE%97tabview%E4%B8%ADlabel%E7%9A%84%E9%AB%98%E5%BA%A6>), translated on 2026-10-23. The article body uses automated translation; app descriptions, historical reading notes and video subtitles receive separate editorial checks. Original publication dates, attribution, code and version notes are retained.

**Before you read** This technical note dates from 2015–2017. The examples have not been rerun as part of this archive update. Published in 2015. The example measures text manually with sizeWithFont.

A method for automatically calculating the height of a label in a tabview

```css
- (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;
} 
```

