Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/120.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何根据子视图正确调整父视图的大小?(例如,包含UITextView的UITableViewCell)_Ios_Objective C_Uitableview_Cocoa Touch_Uitextview - Fatal编程技术网

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
周围添加一些填充

好了!希望有帮助