基于textView中的内容的iOS大小的表格行

我正在尝试在我的表中调整行的大小到textView的高度,以便用户不必滚动textView,而是滚动表格。 到目前为止我已经实现的东西似乎大部分时间都在工作,尽pipe有时候行高有点太小,迫使用户滚动一下textview,或者行高过多,在文本结束后会产生大量的空白。 我怎样才能正确的框架?

码:

- (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; } 

尝试这个

 - (CGFloat)tableView:(UITableView *)tv heightForRowAtIndexPath:(NSIndexPath *)indexPath { NSString* str = ... // here is a string which will be in text view CGSize boundingSize = CGSizeMake(textView.frame.size.width - 20, CGFLOAT_MAX); CGSize textSize = [str sizeWithFont:[UIFont systemFontOfSize:13] constrainedToSize:boundingSize lineBreakMode:NSLineBreakByWordWrapping]; return textSize.height + 40; // add aditional space for margins }