基于textView中内容的iOS尺寸表行 [英] iOS size table row based on content in textView

查看:71
本文介绍了基于textView中内容的iOS尺寸表行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将表格中的行的大小调整为textView的高度,以便用户不必滚动textView,而是滚动表格.尽管有时行高太小,迫使用户稍微滚动文本视图,或者行高太大,文本结束后会产生很多空白,但到目前为止,我实现的大部分时间似乎仍然有效.我怎样才能恰到好处?

I am trying to size a row in my table to the height of a textView so that the user does not have to scroll the textView, but rather scrolls the table. What I have implemented so far seems to work most of the time although sometimes the row height is a little too small, forcing the user to scroll the textview a little, or the row height is too much create a lot of whitespace after the text ends. How can I frame it just right?

代码:

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

推荐答案

尝试一下

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

这篇关于基于textView中内容的iOS尺寸表行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆