Created
September 22, 2013 14:48
-
-
Save alyssais/6660640 to your computer and use it in GitHub Desktop.
My (probably wrong) way of doing dynamic height UITableViewCells.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Next step is to make it work with Dynamic Text.