Resize UITextView in UITableViewCell

I am trying to resize a UITextView that I have in a UITableViewCell. I also want to resize a UITableViewCell. My UITableViewCell resizes correctly, but a UITextView does not. The result is only two lines, and not the size to the desired size. When I initialize it in the cellForRowAtIndexPath method, it looks like this:

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 change 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 happens is to resize the View table correctly, and although the textView frame has the correct size (for example, {{83, 12}, {197, 120}}), the textView has only 2 rows. This does not result in a UITableViewCell size, as I expected.

EDIT: When I try to just change the frame size for a UITextView in a regular UIView that is not a UITableViewCell, it works fine. Therefore, I am not sure what is different in this case.

0
source share
4 answers

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

 [tableView beginUpdates]; [tableView endUpdates]; 

More information can be found at: UITextView in UITableViewCell provides smooth, automatic resizing and hides the keyboard on the iPad, but works on the iPhone

+4
source

I have found a better way to solve this problem.

First, of course, you will want to create your UITextView and add it to your cellView content. I created a UITextView instance variable called "cellTextView". Here is the code 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 the init method. Then, in your textView textIDDidChange, 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; } 
+1
source

I will give you a 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 do not forget to implement automatic resizing for textview

0
source

This is a simple project demonstrating how to use auto-layout to resize cells with iOS 8. Virtually no special code is required. See Readme.md for key points.

github project

0
source

Source: https://habr.com/ru/post/1487460/