Created
October 31, 2012 15:58
-
-
Save hyperspacemark/3987877 to your computer and use it in GitHub Desktop.
Dynamic cell height
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
| @implementation TDLessonViewController | |
| static CGFloat kMinimumLessonStepHeight = 88.0f; | |
| static CGFloat kVerticalTextPadding = 20.0f; | |
| static UIFont *kTextLabelFont; | |
| static CGFloat kTextLabelFontSize = 15.0f; | |
| static CGFloat kTextLabelWidth = 250.0f; | |
| static UILineBreakMode kTextLabelLineBreakMode = UILineBreakModeWordWrap; | |
| static UIFont *kNoteLabelFont; | |
| static CGFloat kNoteLabelFontSize = 16.0f; | |
| static CGFloat kNoteLabelWidth = 300.0f; | |
| static NSString *instructionCellIdentifier = @"InstructionCell"; | |
| static NSString *noteCellIdentifier = @"NoteCell"; | |
| #pragma mark - Initialization | |
| + (void)initialize | |
| { | |
| kTextLabelFont = [UIFont boldSystemFontOfSize:kTextLabelFontSize]; | |
| kNoteLabelFont = [UIFont fontWithName:@"Noteworthy-Bold" size:kNoteLabelFontSize]; | |
| } | |
| #pragma mark - View Lifecycle | |
| - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation | |
| { | |
| return (toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown || toInterfaceOrientation == UIInterfaceOrientationPortrait); | |
| } | |
| - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath | |
| { | |
| UITableViewCell *cell = nil; | |
| LessonStep *lessonStep = [self.lesson.lessonSteps objectAtIndex:indexPath.row]; | |
| if (lessonStep.note) | |
| cell = [self.tableView dequeueReusableCellWithIdentifier:noteCellIdentifier]; | |
| else | |
| cell = [self.tableView dequeueReusableCellWithIdentifier:instructionCellIdentifier]; | |
| [self configureCell:cell forRowAtIndexPath:indexPath]; | |
| return cell; | |
| } | |
| - (void)configureCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath | |
| { | |
| LessonStep *lessonStep = [self.lesson.lessonSteps objectAtIndex:indexPath.row]; | |
| [self configureTextLabel:cell.textLabel forRowAtIndexPath:indexPath]; | |
| if (!lessonStep.note) | |
| [self configureImageView:cell.imageView forRowAtIndexPath:indexPath]; | |
| } | |
| - (void)configureTextLabel:(UILabel *)label forRowAtIndexPath:(NSIndexPath *)indexPath | |
| { | |
| LessonStep *lessonStep = [self.lesson.lessonSteps objectAtIndex:indexPath.row]; | |
| label.lineBreakMode = kTextLabelLineBreakMode; | |
| label.numberOfLines = 0; | |
| label.text = lessonStep.text; | |
| if (lessonStep.note) | |
| { | |
| label.font = kNoteLabelFont; | |
| label.textColor = [UIColor blackColor]; | |
| CGRect textLabelFrame = label.frame; | |
| textLabelFrame.origin.x = 60.0f; | |
| textLabelFrame.size.width = kTextLabelWidth; | |
| label.frame = textLabelFrame; | |
| } | |
| else | |
| { | |
| label.font = kTextLabelFont; | |
| label.textColor = [UIColor lessonTextColor]; | |
| CGRect textLabelFrame = label.frame; | |
| textLabelFrame.origin.x = 10.0f; | |
| textLabelFrame.size.width = kNoteLabelWidth; | |
| label.frame = textLabelFrame; | |
| } | |
| } | |
| #pragma mark - UITableViewDelegate | |
| - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath | |
| { | |
| LessonStep *lessonStep = [self.lesson.lessonSteps objectAtIndex:indexPath.row]; | |
| CGFloat width = (lessonStep.note) ? kNoteLabelWidth : kTextLabelWidth; | |
| UIFont *font = (lessonStep.note) ? kNoteLabelFont : kTextLabelFont; | |
| CGFloat textHeight = [self heightForText:lessonStep.text withWidth:width withFont:font]; | |
| CGFloat paddedHeight = textHeight + kVerticalTextPadding; | |
| if (paddedHeight < kMinimumLessonStepHeight) | |
| return kMinimumLessonStepHeight; | |
| return paddedHeight; | |
| } | |
| #pragma mark - UITableViewDelegate Helpers | |
| - (CGFloat)heightForText:(NSString *)text withWidth:(CGFloat)width withFont:(UIFont *)font | |
| { | |
| CGSize calculatedSize = [text sizeWithFont:font constrainedToSize:CGSizeMake(width, CGFLOAT_MAX) lineBreakMode:kTextLabelLineBreakMode]; | |
| return calculatedSize.height; | |
| } | |
| @end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment