0

我正在尝试将表格中的一行调整为 textView 的高度,以便用户不必滚动 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;
}
4

1 回答 1

0

尝试这个

- (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
}
于 2013-09-15T17:08:54.937 回答