简体   繁体   中英

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. I read a bunch of posts on this but I think I am getting thrown because I am using a STATIC table view.

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.

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:

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

#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?

Please check my answer.

Calculate UITableViewCell height to fit string

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

Your will need to manually calculate new size of you textView , and manually call update for your tableView .

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:

self.appendHeightForTextView = 0;   

And finally set constraints in storyboard for your cell. Do not set bottom constraint of your textView. Only set right, left, and top.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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