I'm struggling with how to dynamically size a uilabel inside a uitableviewcell and also to adjust the height of the table cell depending on the text I put into the label. I want to display a string in its entirety within the tableview cell, always at the same font size for all cells. This seems pretty standard, but I notice a lot of discussion/debate/discontent on stackoverflow. I got started with these posts:
Resize Custom cell with a UILabel on it based on content text from JSON file (@Seya's answer) boundingRectWithSize for NSAttributedString returning wrong size (@JOM's answer)
The functions I'm trying to use are copies of what Seya did, barely modified. However, I am seeing that although the function prints out different heights for different cells, all I am seeing is 1 line of text per label even though they should show many lines. Also, strangely the text from one cell seems to display on top of another cell - not sure whether this is related.
My 2 questions: (1) Why do I only see the first line of text? (2) Why is the UILabel/cell not resizing per the different heights I see printed out in the log (or are they resizing but masked by issue #1)?
Here's the three functions I'm using (these along with a xib - could the xib be the problem?):
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
JudgeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"JudgeCell"];
    if(!cell)
    {
        [tableView registerNib:[UINib nibWithNibName:@"JudgeCell" bundle:nil] forCellReuseIdentifier:@"JudgeCell"];
        cell = [tableView dequeueReusableCellWithIdentifier:@"JudgeCell"];
    }
    cell.username.text = self.object.answerUser[indexPath.row];
    cell.answer.text = self.object.answerArray[indexPath.row];
    NSString *text = cell.answer.text;
    CGSize constraint = CGSizeMake(50, 20000.0f);
    CGSize size = [self frameForText:text sizeWithFont:[UIFont systemFontOfSize:15] constrainedToSize:constraint];
    cell.username.frame = CGRectMake(10, 10, 50, size.height); //MAX(size.height, 14.0f));
    return cell;
}
-(CGSize)frameForText:(NSString*)text sizeWithFont:(UIFont*)font constrainedToSize:(CGSize)size  {
    NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle defaultParagraphStyle] mutableCopy];
    paragraphStyle.lineBreakMode = NSLineBreakByCharWrapping;
    NSDictionary * attributes = @{NSFontAttributeName:font,
                                  NSParagraphStyleAttributeName:paragraphStyle
                                  };
    CGRect textRect = [text boundingRectWithSize:size
                                         options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)
                                      attributes:attributes
                                         context:nil];
    NSLog(@"size is %f ", textRect.size.height);
    return textRect.size;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    NSString *text = self.object.answerArray[indexPath.row];
    CGSize constraint = CGSizeMake(50, 20000.0f);
    CGSize size = [self frameForText:text sizeWithFont:[UIFont systemFontOfSize:15] constrainedToSize:constraint];
    CGFloat height = size.height; //MAX(size.height, 14.0f);
    NSLog(@"size is %f AT INDEX %d", height, indexPath.row);
    return height + 20;
}
Thanks for any advice!
 
    