iqueqiorio
iqueqiorio

Reputation: 1187

Fit Text in UItableView Cell

I have a long label that is my first label, and I want to fit it in my cell. This is what I have but it isn't working.

I have a custom UITabelviewCell with a few labels in it.

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
switch (indexPath.row) {
    case 0:{

        CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);

        CGSize size = [diningHallTimes[indexPath.row][@"description"] sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:NSLineBreakByWordWrapping];

        CGFloat height = MAX(size.height, 44.0f);

            return height + (CELL_CONTENT_MARGIN * 2) + 40;
//            return myStringSize.height;
            break;
        }
        default:
            return 40;
            break;
    }   
}

Here is cell for row

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

DiningInfoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);

CGSize size = [diningHallTimes[indexPath.row][@"description"] sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:NSLineBreakByWordWrapping];

CGFloat height = MAX(size.height, 44.0f);

CGFloat the =  height + (CELL_CONTENT_MARGIN * 2);
cell.descriptionLabel.numberOfLines = 0;

[cell.descriptionLabel setFrame:CGRectMake(20, 0, 280, the)];

NSLog(@"%f", the);


cell.descriptionLabel.text = diningHallTimes[indexPath.row][@"description"];
cell.daysLabel.text = diningHallTimes[indexPath.row][@"days"];
cell.timeLabel.text = diningHallTimes[indexPath.row][@"time"];

return cell;
}

But then this is what my cell looks like

enter image description here

Not sure why this is happening, I am running iOS8, but I need it to work for both is and 7.

Thanks for the help in advance.

Upvotes: 1

Views: 995

Answers (3)

Shobhakar Tiwari
Shobhakar Tiwari

Reputation: 7892

Try [yourLabel sizeToFit] or you may use [yourLabel sizeThatFit:CGFrame];

Upvotes: 1

Mehul Patel
Mehul Patel

Reputation: 23053

You need to calculate boundingRectWithSize: for label text. Also need to calculate number of lines required for updated content also. Try this below method to calculate new frame for label.

- (UILabel *) updateLabelFrame:(UILabel *)label {

    CGRect lblRect = label.frame;
    CGSize maxSize = CGSizeMake(label.frame.size.width, MAXFLOAT);

    CGRect labelRect = [label.text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:label.font} context:nil];

    CGFloat labelHeight = labelRect.size.height;

    // For below line 16 is default height (The height before content is set). Change this value
    // as per your requirement

    int lines = labelHeight / 16; // Here 16 is a fix height (default height) for label.
    [label setNumberOfLines:lines];
    lblRect.size.height = labelHeight;
    [label setFrame:lblRect];

    return label;
}

Edit:

You can place this method any where you want. Like some base class or in same view controller. Here I modified above method which will return label with dynamic frame.

For your case you need to place this method in view controller class. Then call this method in -cellForRowAtIndexPath after content is updated for label. See below code:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    DiningInfoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

    CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);

    CGSize size = [diningHallTimes[indexPath.row][@"description"] sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:NSLineBreakByWordWrapping];

    CGFloat height = MAX(size.height, 44.0f);

    CGFloat the =  height + (CELL_CONTENT_MARGIN * 2);
    cell.descriptionLabel.numberOfLines = 0;

    [cell.descriptionLabel setFrame:CGRectMake(20, 0, 280, the)];

    NSLog(@"%f", the);


    cell.descriptionLabel.text = diningHallTimes[indexPath.row][@"description"];
    cell.daysLabel.text = diningHallTimes[indexPath.row][@"days"];
    cell.timeLabel.text = diningHallTimes[indexPath.row][@"time"];

    // Update descriptionLabel height.

    cell.descriptionLabel = [self updateLabelFrame:cell.descriptionLabel];

    return cell;
}

Upvotes: 2

Arun Balakrishnan
Arun Balakrishnan

Reputation: 1502

You have to set up Autolayout constraints for the label and create a prototype cell with the content. And then you have to return then you have to get the height of the cell.

For reference , you could see the following video https://www.youtube.com/watch?v=6KImie4ZMwk

Upvotes: 1

Related Questions