Sunday, 15 September 2013

iOS size table row based on content in textView

iOS size table row based on content in textView

I am trying to size a row in my table to the height of a textView so that
the user does not have to scroll the textView, but rather scrolls the
table. What I have implemented so far seems to work most of the time
although sometimes the row height is a little too small, forcing the user
to scroll the textview a little, or the row height is too much create a
lot of whitespace after the text ends. How can I frame it just right?
Code:
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
// Set the textview frame
CGRect frame = self.contentTextView.frame;
frame.size.height = [self.summary boundingRectWithSize:CGSizeMake(280,
0) options:(NSStringDrawingUsesLineFragmentOrigin)
attributes:@{NSFontAttributeName:self.contentTextView.font}
context:nil].size.height + 60;
self.contentTextView.frame = frame;
}
- (CGFloat)tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return the height for the rows
if (indexPath.section == 0) {
return 60;
}
if (indexPath.section == 3) {
// Size content row based on text
return [self.summary boundingRectWithSize:CGSizeMake(280, 0)
options:(NSStringDrawingUsesLineFragmentOrigin)
attributes:@{NSFontAttributeName:self.contentTextView.font}
context:nil].size.height + 100;
}
return 44;
}

No comments:

Post a Comment