コンテンツがNSTextAlignmentJustifiedによって整列されるUITextViewがあります。cellForRowAtIndexPathの場合:このテキストビューをセルコンテンツビューに追加し、コンテンツに応じてフレームを変更します。
UITextView *commentsTextView = (UITextView *)[self getCommentField:comment];
[cell.contentView addSubview:commentsTextView];
CGRect tempFrame = commentsTextView.frame;
tempFrame.size.height = commentsTextView.contentSize.height;
commentsTextView.frame = tempFrame;
ここまでは順調ですね。次に、 heightForRowAtIndexPathのビューのcontentSizeを検出する方法は次のとおりです。私は試した
NSString *comment = [[self.allComments objectAtIndex:indexPath.row] objectForKey:@"commentText"];
CGSize constraintSize = CGSizeMake(315.0f, MAXFLOAT);
CGSize size = [comment sizeWithFont:[UIFont systemFontOfSize:14.0]
constrainedToSize:constraintSize
lineBreakMode:NSTextAlignmentJustified];
また、 lineBreakMode:NSLineBreakByWordWrappingなども試しました。ただし、テキストが長い場合、セルの高さは常に低くなります。これは、位置揃えされたテキストのサイズを計算する方法がないように思われるためです(テキストの中央に空白があるため)。フォントは同じです。