Crystal
Crystal

Reputation: 29448

Resizing UITextView in a UITableViewCell

I'm trying to resize a UITextView that I have in a UITableViewCell. I want to resize the UITableViewCell accordingly also. My UITableViewCell resizes properly, but the UITextView does not. It ends up being just two lines instead of sizing to the the correct size. When I initialize it in the cellForRowAtIndexPath method, it looks like:

        UITextView *notesTextView = [[UITextView alloc] initWithFrame:CGRectMake(83, 12, TEXTVIEW_WIDTH, 22)]; 

        notesTextView.tag = TEXTVIEW_TAG;
        notesTextView.delegate = self;

        [cell.contentView addSubview:notesTextView];

When I try to modify it in the textView delegate method, I do:

CGSize size = [textView.text sizeWithFont:textView.font constrainedToSize:CGSizeMake(TEXTVIEW_WIDTH, 460) lineBreakMode:UILineBreakModeWordWrap];
NSLog(@"size: %@", NSStringFromCGSize(size)); // has the correct size
if (size.height > self.notesRowHeight) {
    self.notesRowHeight = size.height;
    UITextView *aTextView = (UITextView *)[self.view viewWithTag:TEXTVIEW_TAG];
    CGRect textViewFrame = CGRectMake(aTextView.frame.origin.x, aTextView.frame.origin.y, TEXTVIEW_WIDTH, size.height);
    aTextView.frame = textViewFrame;
    aTextView.contentSize = size;
    [aTextView sizeToFit];
    [aTextView sizeThatFits:size];
    NSLog(@"%@", NSStringFromCGRect(aTextView.frame)); // has the correct height

So what ends up happening is the tableView resize properly, and even though the textView frame is the correct size (e.g. {{83, 12}, {197, 120}}), the textView only has 2 lines. It does not end up taking the size of the UITableViewCell like I intended.

EDIT: When I try to just resize the frame for the UITextView in a normal UIView that is not a UITableViewCell, it works just fine. So I'm not sure what's different in this case.

Upvotes: 0

Views: 2952

Answers (4)

Chuck Krutsinger
Chuck Krutsinger

Reputation: 2930

This is a simple project that demonstrates how to use auto layout to resize the cells as of iOS 8. Almost no special code required. See readme.md for key points.

project on github

Upvotes: 0

Hector Matos
Hector Matos

Reputation: 1056

I found the best way to solve this.

First off, of course, you're going to want to create your UITextView and add it to your cell's contentView. I created an instance variable of UITextView called "cellTextView" Here is the code that I used:

- (UITableViewCell *)tableView:(UITableView *)tableView fileNameCellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];

    if (!cellTextView) {
        cellTextView = [[UITextView alloc] initWithFrame:CGRectMake(5.0, 5.0, cell.bounds.size.width - 30.0, cell.bounds.size.height - 10.0)]; // I use these x and y values plus the height value for padding purposes.
    }
    [cellTextView setBackgroundColor:[UIColor clearColor]];
    [cellTextView setScrollEnabled:FALSE];
    [cellTextView setFont:[UIFont boldSystemFontOfSize:13.0]];
    [cellTextView setDelegate:self];
    [cellTextView setTextColor:[UIColor blackColor]];
    [cellTextView setContentInset:UIEdgeInsetsZero];
    [cell.contentView addSubview:cellTextView];

    return cell;
}

Then, create an int variable called numberOfLines and set the variable to 1 in your init method. Afterwards, in your textViewDelegate's textViewDidChange method, use this code:

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

    float height = 44.0;
    height += (textView.font.lineHeight * (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];

    [cellTextView setContentInset:UIEdgeInsetsZero];
}    

Finally, paste this code into your heightForRowAtIndexPath method:

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

Upvotes: 1

Crystal
Crystal

Reputation: 29448

I found the answer. I should not reload the row, but just call

[tableView beginUpdates];
[tableView endUpdates];

More information can be found at: UITextView in a UITableViewCell smooth auto-resize shows and hides keyboard on iPad, but works on iPhone

Upvotes: 4

J S Rodrigues
J S Rodrigues

Reputation: 471

I will give you an different view....

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
       .
       .
       if(_YOUR_ROW_ && _YOUR_SECTION_)
       {

           cell.textView.text = Text_Of_TextView;
       }
       .
       .

}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
   .
   .
   if(_YOUR_ROW_ && _YOUR_SECTION_)
   {


       UIFont * FontSize = [UIFont systemFontOfSize:14.0];  
       CGSize textSize = [Text_Of_TextView sizeWithFont:FontSize constrainedToSize:CGSizeMake(Width_OF_textView, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap];

       return textSize.height+PADDING;
   }
   .
   .
}




-(void)textViewDidEndEditing:(UITextView *)textView
{

    [Text_Of_TextView setText:textView.text];


 CGRect frame = textViewInRow.frame;
        frame.size.height = textViewInRow.contentSize.height;
        textViewInRow.frame = frame;
 [TableView_InView reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:_YOUR_ROW_ inSection:_YOUR_SECTION_]] withRowAnimation:UITableViewRowAnimationNone];

}

and also dont forget to implement auto resizing behaviour for textview

Upvotes: 0

Related Questions