【问题标题】:iOS size table row based on content in textViewiOS 大小表行基于 textView 中的内容
【发布时间】:2013-09-19 19:40:58
【问题描述】:

我正在尝试将表格中的一行调整为 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;
}

【问题讨论】:

    标签: ios textview tableview frame cgrect


    【解决方案1】:

    试试这个

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

    【讨论】:

    • sizeWithFont 在 iOS7 中已弃用。这就是使用 boundingRectWithSize 的原因。
    猜你喜欢
    • 1970-01-01
    • 2015-11-05
    • 2017-11-14
    • 1970-01-01
    • 1970-01-01
    • 2013-11-08
    • 1970-01-01
    • 2014-12-28
    • 1970-01-01
    相关资源
    最近更新 更多