Ios 如何根据子视图正确调整父视图的大小?(例如,包含UITextView的UITableViewCell)
在我的例子中,如何调整Ios 如何根据子视图正确调整父视图的大小?(例如,包含UITextView的UITableViewCell),ios,objective-c,uitableview,cocoa-touch,uitextview,Ios,Objective C,Uitableview,Cocoa Touch,Uitextview,在我的例子中,如何调整UITableViewCell的大小以适应它所包含的UITextView的高度 我目前正试图使用以下代码实现这一点: - (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { // init the font that we use UIFont *font = [UIFont fontWithName:@"Source
UITableViewCell
的大小以适应它所包含的UITextView
的高度
我目前正试图使用以下代码实现这一点:
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
// init the font that we use
UIFont *font = [UIFont fontWithName:@"SourceSansPro-Regular" size:16];
if (indexPath.section == 0) {
// get the text size of the thread
CGSize textSize = [_thread.text sizeWithFont:font constrainedToSize:CGSizeMake(280, CGFLOAT_MAX) lineBreakMode:UILineBreakModeWordWrap];
return textSize.height + 20;
}
// default
return 60;
}
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
// init the font that we use
UIFont *font = [UIFont fontWithName:@"SourceSansPro-Regular" size:16];
if (indexPath.section == 0) {
// get the text size of the thread
CGSize textSize = [_thread.text sizeWithFont:font constrainedToSize:CGSizeMake(280 - 16, CGFLOAT_MAX) lineBreakMode:UILineBreakModeWordWrap];
// text height + padding
return textSize.height + 40;
} else if (indexPath.section == 1) {
// get the comment for this row
Comment *comment = [_comments objectAtIndex:indexPath.row];
// get the heights of the text areas
CGSize textSize = [comment.text sizeWithFont:font constrainedToSize:CGSizeMake(280 - 16, CGFLOAT_MAX) lineBreakMode:UILineBreakModeWordWrap];
return textSize.height + 40;
}
return 60;
}
我觉得如果我可以在这个方法中访问UITableViewCell
,那么我就可以在其中使用UITextView
的contentSize
属性,并返回该属性。但我认为在执行流程的这一点上,这是不可能的
我不能仅仅调整缓冲区,因为它不能随着get size函数返回的文本大小进行缩放。文本越短,缓冲区越大
这是在调整UITableViewCell
的高度,但并没有显示其内部的所有UITextView
。我已经在sizeWithFont:constrainedToSize:lineBreakMode:
方法中指定了UITextView
的宽度,即280
。我已经指定,我希望基本上没有最大高度,这样它就可以使用CGFLOAT\u MAX
常量,尽可能多地换行。我在方法调用的结果高度上附加了一个20
单位的缓冲区,因为在我的情节提要中,UITextView
上方有一个20
单位缓冲区,我还希望在其下方有一个20
单位缓冲区
然而,所有这些的最终结果仍然是文本被剪掉。下面是我所说内容的图片:
有什么想法吗?我继续回答我的问题。答案有两个步骤
首先,在tableView:cellforrowatinexpath:
方法中的逻辑期间,调整UITextView
的大小。确保在对UITextView
中的文本进行任何更改后执行此操作。在UITableViewCell
的子类中,我使用以下方法重新调整我的UITextView
的大小,其中self.text
是文本视图:
- (void)resizeTextView {
// get the current frame size
CGRect frame = self.text.frame;
// set it to the height of the text view
frame.size.height = self.text.contentSize.height;
// set the new size of the text view
self.text.frame = frame;
}
接下来,我指定tableView:heightforrowatinexpath:
方法内部的UITableViewCell
的高度。这就是大部分谜团存在的地方,因为要获得此行的高度,您必须计算将要在该行中显示的任何文本的高度,以及计算该行高度所需的任何其他内容。在我的场景中,is是一个UITextView
。在此方法中,如果无法访问UITableViewCell
实例,则必须使用类似sizeWithFont:constrainedToSize:lineBreakMode:
。但是,我注意到这对我来说并没有达到一致的高度。或者至少,高度似乎没有为我在UITextView
中的文本腾出足够的空间。但这就是问题所在。与sizeWithFont
方法相比,文本视图具有额外的左右填充以及不同的行高或其他内容
因此,在对sizeWithFont
方法的结果进行了一段时间的伪造之后,我得到了以下代码:
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
// init the font that we use
UIFont *font = [UIFont fontWithName:@"SourceSansPro-Regular" size:16];
if (indexPath.section == 0) {
// get the text size of the thread
CGSize textSize = [_thread.text sizeWithFont:font constrainedToSize:CGSizeMake(280, CGFLOAT_MAX) lineBreakMode:UILineBreakModeWordWrap];
return textSize.height + 20;
}
// default
return 60;
}
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
// init the font that we use
UIFont *font = [UIFont fontWithName:@"SourceSansPro-Regular" size:16];
if (indexPath.section == 0) {
// get the text size of the thread
CGSize textSize = [_thread.text sizeWithFont:font constrainedToSize:CGSizeMake(280 - 16, CGFLOAT_MAX) lineBreakMode:UILineBreakModeWordWrap];
// text height + padding
return textSize.height + 40;
} else if (indexPath.section == 1) {
// get the comment for this row
Comment *comment = [_comments objectAtIndex:indexPath.row];
// get the heights of the text areas
CGSize textSize = [comment.text sizeWithFont:font constrainedToSize:CGSizeMake(280 - 16, CGFLOAT_MAX) lineBreakMode:UILineBreakModeWordWrap];
return textSize.height + 40;
}
return 60;
}
在size constraint参数中,我减去了16个单位,因为UITextView
在左右两侧都有大约8个单位的填充。这将使我们的结果更接近我们对文本高度的预期
我还为最终高度(40个单位)添加了一些填充,因为行本身需要在UITextView
周围添加一些填充
好了!希望这能有所帮助。我将继续回答我的问题。答案有两个步骤
首先,在tableView:cellforrowatinexpath:
方法中的逻辑期间,调整UITextView
的大小。确保在对UITextView
中的文本进行任何更改后执行此操作。在UITableViewCell
的子类中,我使用以下方法重新调整我的UITextView
的大小,其中self.text
是文本视图:
- (void)resizeTextView {
// get the current frame size
CGRect frame = self.text.frame;
// set it to the height of the text view
frame.size.height = self.text.contentSize.height;
// set the new size of the text view
self.text.frame = frame;
}
接下来,我指定tableView:heightforrowatinexpath:
方法内部的UITableViewCell
的高度。这就是大部分谜团存在的地方,因为要获得此行的高度,您必须计算将要在该行中显示的任何文本的高度,以及计算该行高度所需的任何其他内容。在我的场景中,is是一个UITextView
。在此方法中,如果无法访问UITableViewCell
实例,则必须使用类似sizeWithFont:constrainedToSize:lineBreakMode:
。但是,我注意到这对我来说并没有达到一致的高度。或者至少,高度似乎没有为我在UITextView
中的文本腾出足够的空间。但这就是问题所在。与sizeWithFont
方法相比,文本视图具有额外的左右填充以及不同的行高或其他内容
因此,在对sizeWithFont
方法的结果进行了一段时间的伪造之后,我得到了以下代码:
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
// init the font that we use
UIFont *font = [UIFont fontWithName:@"SourceSansPro-Regular" size:16];
if (indexPath.section == 0) {
// get the text size of the thread
CGSize textSize = [_thread.text sizeWithFont:font constrainedToSize:CGSizeMake(280, CGFLOAT_MAX) lineBreakMode:UILineBreakModeWordWrap];
return textSize.height + 20;
}
// default
return 60;
}
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
// init the font that we use
UIFont *font = [UIFont fontWithName:@"SourceSansPro-Regular" size:16];
if (indexPath.section == 0) {
// get the text size of the thread
CGSize textSize = [_thread.text sizeWithFont:font constrainedToSize:CGSizeMake(280 - 16, CGFLOAT_MAX) lineBreakMode:UILineBreakModeWordWrap];
// text height + padding
return textSize.height + 40;
} else if (indexPath.section == 1) {
// get the comment for this row
Comment *comment = [_comments objectAtIndex:indexPath.row];
// get the heights of the text areas
CGSize textSize = [comment.text sizeWithFont:font constrainedToSize:CGSizeMake(280 - 16, CGFLOAT_MAX) lineBreakMode:UILineBreakModeWordWrap];
return textSize.height + 40;
}
return 60;
}
在size constraint参数中,我减去了16个单位,因为UITextView
在左右两侧都有大约8个单位的填充。这将使我们的结果更接近我们对文本高度的预期
我还为最终高度(40个单位)添加了一些填充,因为行本身需要在UITextView
周围添加一些填充
好了!希望有帮助