I have problems with truncated text in UILabels within a tableviewcell. So far, I have read and tested the solutions in the following links (among many others similar):
- Multiple UILabels inside a self sizing UITableViewCell
- ios8 autolayout: BOTH multi-line(maybe 0 line) label
- Using Auto Layout in UITableView for dynamic cell layouts & variable row heights
but I cannot seem to get it to work on first load if I set up the cell with “standard” top, leading, trailing and bottom auto layout constraints. I can, however, if I uncheck the “Relative to margin” for the superview item and use e.g. Equals: Default
If I rotate the device or scroll down and up again, it works for both cases.
The labels are connected with outlets on a custom cell and are placed above each other vertically in the prototype cell; they have three superview constraints each, with a “standard” vertical distance between the labels. The hugging priority is set higher on the topmost label to get rid of the warning in the IB, even though it does not matter for the result.
Relevant code:
TableViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1) {
self.tableView.estimatedRowHeight = self.tableView.rowHeight;
self.tableView.rowHeight = UITableViewAutomaticDimension; }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
TableViewCellIB *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
[cell adjustSizeToMatchWidth:CGRectGetWidth(self.tableView.frame)];
cell.titleLabel.text = @"Veeeeeeeery looooooooooooooooong Tiiiiitle";
cell.messageLabel.text = @"Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda. Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.";
[cell setNeedsUpdateConstraints];
[cell updateConstraintsIfNeeded];
return cell; }
TableViewCellIB.m
- (void)adjustSizeToMatchWidth:(CGFloat)width {
CGRect rect = self.frame;
rect.size.width = width;
self.frame = rect;
rect = self.contentView.bounds;
rect.size.height = 99999.0;
rect.size.width = 99999.0;
self.contentView.bounds = rect; }
One small problem with “Relative to margin” unchecked and "Equals: Default", is that, even though the text is not truncated in the labels, the cell does not look very nice with these setting, since the margins are far to large in my opinion. Is there some other “default” way to get this to work from Storyboard, or do I have to use “magic numbers" for the Equals in the constraints?
Furthermore, how would I get it to work if I do this programmatically?
As far as I understand, the standard visual format "H:|-[topLabel]-|” defaults to “Relative to margin” since iOS 8.
Thanks in advance!