Note that there are some explanatory texts on larger screens.

plurals
  1. PONSString sizeWithFont:constrainedToSize: computing wrong height
    primarykey
    data
    text
    <p>I have a Subtitle style UITableViewCell which height changes dynamically depending on the length of the text for each field. The problem is that the textLabel's height (CGSize size) does not increase if the label has multiple lines.</p> <p><img src="https://i.stack.imgur.com/fZNlC.png" alt="http://s10.postimg.org/6urher7s9/text_Label.png"></p> <p>The weird part is that the detailTextLabel's height is increasing as it should (CGSize size2). The code to calculate both heights are identical.</p> <p><img src="https://i.stack.imgur.com/HtGz9.png" alt="http://s21.postimg.org/m0af4gyw7/detail.png"></p> <p>Here is my function:</p> <pre><code>-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { SETLISTFMNS0Song *song = [[[selectedSetlist.sets objectAtIndex:indexPath.section] songs]objectAtIndex:indexPath.row]; CGSize size = [song.name sizeWithFont:[UIFont fontWithName:setlistFont size:labelFontSize] constrainedToSize:CGSizeMake(self.setsTable.bounds.size.width, CGFLOAT_MAX)]; NSLog(@"Label: \"%@\" \tLabel Size: %f W %f H", song.name, size.width, size.height); NSMutableString *detail; if ([song cover]) { detail = [[NSMutableString alloc] initWithFormat:@"(%@ cover)", [[song cover] name]]; } if ([song with]) { if (!detail) { detail = [[NSMutableString alloc] initWithFormat:@"(with %@)", [[song with] name]]; } else { [detail appendFormat:@" (with %@)", [[song with] name]]; } } if ([song info]) { if (!detail) { detail = [[NSMutableString alloc] initWithFormat:@"(%@)", [song info]]; } else { [detail appendFormat:@" (%@)", [song info]]; } } if (detail.length != 0) { CGSize size2 = [detail sizeWithFont:[UIFont fontWithName:setlistFont size:detailFontSize] constrainedToSize:CGSizeMake(self.setsTable.bounds.size.width, CGFLOAT_MAX)]; size.height += size2.height; NSLog(@"Detail Label: \"%@\" \tDetail Label Size: %f W %f H", detail, size2.width, size2.height); } return size.height + 5; } </code></pre> <p>I am also setting both textLabel's numberOfLines property to 0 in cellForRowAtIndexPath to support multiple lines:</p> <pre><code>cell.textLabel.numberOfLines = 0; cell.detailTextLabel.numberOfLines = 0; </code></pre> <p><strong>UPDATE:</strong> Thanks to @josh I now understand why this is happening. I had the width constraints set to the width of the UITableView, which is too wide. Anyone know how to find the width of the UILabel before it is created? HA!</p> <p>Thanks!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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