0

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
                                                      options:opts
                                                   attributes:[cell.detailTextLabel.attributedText attributesAtIndex:0 effectiveRange:nil]
                                                      context:nil].size;
    CGFloat height = size.height;
    size = [cell.textLabel.text boundingRectWithSize:boundingRect
                                         options:opts
                                      attributes:[cell.textLabel.attributedText attributesAtIndex:0 effectiveRange:nil]
                                         context:nil].size;
    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?

SIMEL
  • 8,745
  • 28
  • 84
  • 130
  • why do you have to set the row height?? Why don't you use estimatedRowHeight ? – Fay007 Jan 08 '17 at 12:51
  • Maybe use Autolayout: http://stackoverflow.com/questions/18746929/using-auto-layout-in-uitableview-for-dynamic-cell-layouts-variable-row-heights – koen Jan 08 '17 at 15:45

2 Answers2

1

From method boundingRectWithSize:options:attributes:context:

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;
pkamb
  • 33,281
  • 23
  • 160
  • 191
guidev
  • 2,695
  • 2
  • 23
  • 44
0

Try with the boundingRect method of NSAttributedString (Swift 3)

//change this value with the width of your UITableViewCell
let availableWidth:CGFloat = 355 

let stringBoundingRect = NSAttributedString(string: "your string").boundingRect(with: CGSize(width: availableWidth, height: CGFloat.greatestFiniteMagnitude), options: [.usesDeviceMetrics, .usesLineFragmentOrigin], context: nil)

let height = stringBoundingRect.height

In Objective-C:

// change this value with the width of your UITableViewCell
CGFloat availableWidth = 355;

NSAttributedString *attrStr = [[NSAttributedString alloc] initWithString:@"your string"];
CGRect stringBoundingRect = [attrStr boundingRectWithSize:CGSizeMake(availableWidth, CGFLOAT_MAX) options: NSStringDrawingUsesLineFragmentOrigin| NSStringDrawingUsesDeviceMetrics context:nil];

CGFloat height = stringBoundingRect.size.height;
Domenico
  • 1,331
  • 18
  • 22