gpt4 book ai didi

ios - 如何根据 subview 正确调整父 View 的大小? (例如包含 UITextView 的 UITableViewCell)

转载 作者:行者123 更新时间:2023-11-29 13:30:59 24 4
gpt4 key购买 nike

在我的例子中,我将如何调整 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;

}

我觉得如果我可以在这个方法中访问 UITableViewCell,那么我可以使用 UITextViewcontentSize 属性在其中,然后返回。但我认为在执行流程的这一点上这是不可能的。

我不能只调整缓冲区,因为它不会随着 get size 函数返回的文本大小进行缩放。文本越短,缓冲区大小越大越明显。

这是在调整 UITableViewCell 的高度,但并未显示其中的所有 UITextView。我在 sizeWithFont:constrainedToSize:lineBreakMode: 方法中指定了 UITextView 的宽度,即 280。我已经指定我希望基本上没有最大高度,以便它可以使用 CGFLOAT_MAX 常量根据需要自动换行。我将 20 单位的缓冲区附加到方法调用的结果高度,因为 UITextView 上方有一个 20 单位缓冲区,在我的Storyboard,我还想在它下面有一个 20 单元缓冲区。

不过,所有这一切的最终结果仍然是文本被剪掉了。这是我正在谈论的图片:

clipped cell contents

有什么想法吗?

最佳答案

我要继续回答我的问题。答案分两个步骤。

首先,在 tableView:cellForRowAtIndexPath: 方法的逻辑中调整 UITextView 的大小。确保在 UITextView 中的文本进行任何更改后执行此操作。我在 UITableViewCell 的子类中使用以下方法调整了我的 UITextView 的大小,其中 self.text 是 TextView :

- (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:heightForRowAtIndexpath: 方法中指定了 UITableViewCell 的高度。这是最神秘的地方,因为要获得该行的高度,您必须计算将要显示在该行中的任何文本的高度,以及您需要计算其高度的任何其他内容。在我的场景中,它是一个 UITextView。如果无法访问此方法中的 UITableViewCell 实例,您将不得不使用类似 sizeWithFont:constrainedToSize:lineBreakMode: 的内容。但是,我注意到这并没有为我返回一致的高度。或者至少,高度似乎无法为 UITextView 中的文本腾出足够的空间。但这就是问题所在。与 sizeWithFont 方法使用的 TextView 相比, TextView 具有额外的左右填充以及不同的行高等。

因此,在对 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 - 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;

}

在大小约束参数中,我减去 16 个单位,因为 UITextView 在左侧和右侧都有大约 8 个单位的填充。这将使我们的结果更接近我们对文本高度的预期。

我还在 40 个单位的最终高度上添加了一些填充,因为该行本身需要在 UITextView 周围填充一些。

给你!希望对您有所帮助。

关于ios - 如何根据 subview 正确调整父 View 的大小? (例如包含 UITextView 的 UITableViewCell),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11927632/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com