简体   繁体   English

在静态表格视图中动态调整文本视图和表格视图单元的大小

[英]Resize text view and table view cell dynamically in static table view

I have a static table view that I am using as a sot of data entry for my app. 我有一个静态表格视图,正在用作我的应用程序的数据输入源。 One of those cells has a UITextView in it, which needs to grow and shrink dynamically as the user types, as well as the cell growing/shrinking logically with it. 其中一个单元格具有一个UITextView,该UITextView需要随着用户类型的变化而动态增长和缩小,以及与此同时逻辑上增长/缩小的单元格。 I read a bunch of posts on this but I think I am getting thrown because I am using a STATIC table view. 我读了很多关于此的文章,但是我认为我因为使用​​STATIC表视图而被抛出。

Here is my resizing attempt (its pretty much a total fail): 这是我调整大小的尝试(几乎完全失败):

- (void)textViewDidChange:(UITextView *)textView
{
    self.numberOfLines = (textView.contentSize.height / textView.font.lineHeight) - 1;

    float height = 44.0;
    height += (textView.font.lineHeight * (self.numberOfLines - 1));

    CGRect textViewFrame = [textView frame];
    textViewFrame.size.height = height - 10.0; //The 10 value is to retrieve the same height padding I inputed earlier when I initialized the UITextView
    [textView setFrame:textViewFrame];

    [self.tableView beginUpdates];
    [self.tableView endUpdates];

    [self.messageTextView setContentInset:UIEdgeInsetsZero];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    float height = 44.0;
    if (self.messageTextView) {
        height += (self.messageTextView.font.lineHeight * (self.numberOfLines - 1));
    }
    return height;
}

I'll take any tips! 我会采取任何提示! Thanks. 谢谢。

You don't have to do anything just go through this code:- There is a new function to replace sizeWithFont, which is boundingRectWithSize. 您无需执行任何操作,只需通过以下代码即可:-有一个新函数可以替换sizeWithFont,即boundingRectWithSize。

Add the following function to my project, which makes use of the new function on iOS7 and the old one on iOS lower than 7. It has basically the same syntax as sizeWithFont: 在我的项目中添加以下函数,该函数利用iOS7上的新功能以及低于7的iOS上的旧功能。它的语法与sizeWithFont基本相同:

   -(CGSize)text:(NSString*)text sizeWithFont:(UIFont*)font constrainedToSize:(CGSize)size{
        if(IOS_NEWER_OR_EQUAL_TO_7){
            NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                              font, NSFontAttributeName,
                                              nil];

            CGRect frame = [text boundingRectWithSize:size
                                              options:(NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading)
                                           attributes:attributesDictionary
                                              context:nil];

            return frame.size;
        }else{
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
            return [text sizeWithFont:font constrainedToSize:size];
#pragma clang diagnostic pop
        }
    }

You can add that IOS_NEWER_OR_EQUAL_TO_7 on your prefix.pch file in your project as: 您可以将IOS_NEWER_OR_EQUAL_TO_7添加到项目中的prefix.pch文件中,如下所示:

#define IOS_NEWER_OR_EQUAL_TO_7 ( [ [ [ UIDevice currentDevice ] systemVersion ] floatValue ] >= 7.0 )

I have referred from this link:- 我从此链接中引用:

UITableViewCell with UITextView height in iOS 7? 在iOS 7中具有UITextView高度的UITableViewCell?

Please check my answer. 请检查我的答案。

Calculate UITableViewCell height to fit string 计算UITableViewCell高度以适合字符串

Here label is used to calculate the height of cell. 这里的标签用于计算像元的高度。

When you done with editing of Text in textfield - (void)textViewDidEndEditing:(UITextView *)textView call the tableview reloadData function to update the cell as per text entered in textview 完成文本字段中的文本编辑后- (void)textViewDidEndEditing:(UITextView *)textView调用tableview reloadData函数以根据在textview中输入的文本更新单元格

Your will need to manually calculate new size of you textView , and manually call update for your tableView . 您将需要手动计算textView新大小,并手动为tableView调用update。

static CGFloat cellHeight = 85;//from story board height to
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];
    if (indexPath.row == 0) {
        return cellHeight + [self appendTextViewNewHeight];
    }
    return cell.bounds.size.height;
}

-(CGFloat) appendTextViewNewHeight {
    return _appendHeightForTextView;
}

static CGFloat textViewHeightStartHeight = 33;//from storiboard height
- (void)textViewDidChange:(UITextView *)textView;
{
    CGFloat fixedWidth = textView.frame.size.width;
    CGSize newSize = [textView sizeThatFits:CGSizeMake(fixedWidth, MAXFLOAT)];
    CGRect newFrame = textView.frame;
    newFrame.size = CGSizeMake(fmaxf(newSize.width, fixedWidth), newSize.height);
    if (newFrame.size.height >= textViewHeightStartHeight) {
        CGFloat newAppend = newFrame.size.height - textViewHeightStartHeight;
        if (newAppend != self.appendHeightForTextView) {
            self.appendHeightForTextView = newAppend;
            [self.tableView beginUpdates];
            [self.tableView endUpdates];
        }
    }
}

Also you need to set delegates on storyboards on in the code. 另外,您还需要在代码中的情节提要上设置委托。 in viedDidLoad you need to do next things: 在viedDidLoad中,您需要执行以下操作:

self.appendHeightForTextView = 0;   

And finally set constraints in storyboard for your cell. 最后在您的单元的情节提要中设置约束。 Do not set bottom constraint of your textView. 不要设置textView的底部约束。 Only set right, left, and top. 仅设置右,左和上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM