Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Although you can calculate heights for labels contained in table view cells, truly dynamically, in '- layoutSubviews' of a UITableViewCell subclass, there's no similar way of doing this (that I know of) for cell heights in '- tableView:heightForRowAtIndexPath:' of a table view delegate.</p> <p>Consider this:</p> <pre><code>- (void)layoutSubviews { [super layoutSubviews]; CGSize size = [self.textLabel.text sizeWithFont:self.textLabel.font constrainedToSize:CGSizeMake(self.textLabel.$width, CGFLOAT_MAX) lineBreakMode:self.textLabel.lineBreakMode]; self.textLabel.$height = size.height; } </code></pre> <p>Unfortunately though, by the time '- tableView:heightForRowAtIndexPath:' is called, that is too early, because <code>cell.textLabel.frame</code> is yet set to <code>CGRectZero = {{0, 0}, {0, 0}}</code>.</p> <p>AFAIK you won't be able to do this neither with content view's frame, nor summing up individual labels' frames...</p> <p>The only way I can think of is to come up with a convenience class, methods, constants, or such that will try to cover up all possible width in any device orientation, on any device:</p> <pre><code>@interface UITableView (Additions) @property (nonatomic, readonly) CGFloat padding; @end @implementation UITableView (Additions) - (CGFloat)padding { if (self.formStyle == PTFormViewStylePlain) { return 0; } if (self.$width &lt; 20.0) { return self.$width - 10.0; } if (self.$width &lt; 400.0 || [[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) { return 10.0; } return MAX(31.0, MIN(45.0, self.$width * 0.06)); } @end </code></pre> <p>Also note that, recently we also have new iPhone 5's 4-inch width (568 instead of 480) in landscape orientation.</p> <p>This whole thing is pretty disturbing, I know... Cheers.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload