We have answer of your question!

100% solved queries, no empty question

Question: Why is the height of UITableViewCell is smaller than the height of its text?


I have a UITableView with UITableViewCells, I set the height of each cell in the function heightForRowAtIndexPath in the fallowing way:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
    NSStringDrawingOptions opts = (NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading);
    CGSize boundingRect = CGSizeMake(450.f, CGFLOAT_MAX);
    CGSize size = [cell.detailTextLabel.text boundingRectWithSize:boundingRect
                                                   attributes:[cell.detailTextLabel.attributedText attributesAtIndex:0 effectiveRange:nil]
    CGFloat height = size.height;
    size = [cell.textLabel.text boundingRectWithSize:boundingRect
                                      attributes:[cell.textLabel.attributedText attributesAtIndex:0 effectiveRange:nil]
    height += size.height;
    return height;

But the cell that I get is too small for the text, and longer text doesn't fit:

if the text doesn't fit you must acquit

The text that is written in cell is set in the following way:

NSString *nickName = @"some nickname"; // gets the nickname
NSString title = [NSString stringWithFormat:@"%@ wrote:", nickName];
NSString *detail = @"some text"; // gets the content of the message

[cell.textLabel setText: title];
[cell.detailTextLabel setText: detail];
cell.detailTextLabel.numberOfLines = 0;
cell.detailTextLabel.lineBreakMode = NSLineBreakByWordWrapping;

Why is the height of the cell too short?

Question author Ilya-melamed | Source



From https://developer.apple.com/reference/foundation/nsstring/1524729-boundingrectwithsize :

"This method returns the actual bounds of the glyphs in the string"

What is missing is the margins of the cell, which can be accessed with cell.layoutMargins:

height += cell.layoutMargins.top + cell.layoutMargins.bottom;
Answer author Guidev

Tickanswer.com is providing the only single recommended solution of the question Why is the height of UITableViewCell is smaller than the height of its text? under the categories i.e ios , objective-c , uitableview , . Our team of experts filter the best solution for you.

Related Search Queries:

You may also add your answer

Thanks for contributing an answer to Tick Answer!