I have custom UITableViewCells that contain UITextViews. In previous versions of iOS I dynamically sized the table view cells like this:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Get the appropriate content string
NSString *sectionKey = [self.orderedSectionKeys objectAtIndex:indexPath.section];
NSDictionary *infoDictionary = [[self.tableViewData objectForKey:sectionKey] objectAtIndex:indexPath.row];
NSString *textViewString = [infoDictionary objectForKey:@"description"];
// Calculate the UITextView height
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, kInfoDefaultTableViewCellTextViewWidth, kInfoDefaultTableViewCellTextViewHeight)];
textView.font = [UIFont fontWithName:kInfoDefaultTableViewCellTextViewFont
size:kInfoDefaultTableViewCellTextViewFontSize];
textView.text = textViewString;
[textView layoutSubviews]; // Call this so that the content size is set
CGFloat height = textView.contentSize.height;
height += kInfoDefaultTableViewCellHeightBuffer;
return height;
}
This is still working for me in iOS 7, except when the textView string contains the new line characters: \n. Then contentSize.height is too small, like it is not adding the new lines but treating \n as characters. In the actual table view, the new lines are added.
Does anyone know how I can get the appropriate height with the new lines? I have tried the technique in this solution: https://stackoverflow.com/a/18818036/472344, but I am getting the same result as what I was using in the posted code.