Skip to content

Instantly share code, notes, and snippets.

@alyssais
Created September 22, 2013 14:48
Show Gist options
  • Save alyssais/6660640 to your computer and use it in GitHub Desktop.
Save alyssais/6660640 to your computer and use it in GitHub Desktop.
My (probably wrong) way of doing dynamic height UITableViewCells.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CGFloat TEXT_VIEW_MARGIN_X = 14; // gap between edge of cell and edge of text view
CGFloat TEXT_VIEW_INSET_X = 5; // gap between edge of text view and text inside
CGFloat TEXT_VIEW_INSET_Y = 9; // gap between top/bottom of text view and text inside
NSString *text = [self lorem];
CGSize constraint = CGSizeMake(tableView.bounds.size.width - (TEXT_VIEW_MARGIN_X + TEXT_VIEW_INSET_X) * 2, CGFLOAT_MAX);
CGSize size = [text boundingRectWithSize:constraint options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:17]} context:nil].size;
return size.height + TEXT_VIEW_INSET_Y * 2;
}
@alyssais
Copy link
Author

Next step is to make it work with Dynamic Text.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment