UITextView Alignment issue inside UITableViewCell AutoLayout

| | August 7, 2015

I am designing an iOS Screen that looks something like this:
enter image description here

What I want to do:

  1. Text size of the text view should when user starts editing. And it should go back to its normal size after editing is done.

  2. When the user is editing the textview the full text of the text view should be visible and in other case at most two lines should be visible.

  3. Size of the UITableViewCell should be maintained according to the size of the textview when the editing is going on.

Here is the my code :

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  NSString *reuseIdentifier = kRateViewCellID;
  if (!self.customCell) {
    self.customCell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
  }

  self.customCell.productDetailLabel.text =  @"Hello the product Name goes here so you can mention it here";
  self.customCell.commentTextView.text = [testData objectAtIndex:indexPath.row];

  // Configure the cell for this indexPath
  self.customCell.commentTextView.font = [UIFont openSansRegularFontWithSize:12];
  self.customCell.commentTextView.textContainer.maximumNumberOfLines = 2;
  self.customCell.commentTextView.textContainer.lineBreakMode = NSLineBreakByTruncatingTail;
  [self.customCell.commentTextView setScrollEnabled:NO];
  if(indexPath.row == selectedIndex) {
    self.customCell.commentTextView.textContainer.maximumNumberOfLines = 0;
    self.customCell.commentTextView.font = [UIFont openSansRegularFontWithSize:16];
    self.customCell.commentTextView.textContainer.lineBreakMode = NSLineBreakByCharWrapping;
  }
  [self.customCell.commentTextView sizeToFit];
  [self.customCell setNeedsLayout];
  [self.customCell layoutIfNeeded];
  CGFloat height = [self.customCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
  height += 1;

  return height;
}

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
  textView.textContainer.maximumNumberOfLines = 0;
  textView.textContainer.lineBreakMode = NSLineBreakByCharWrapping;
  textView.font = [UIFont openSansRegularFontWithSize:16];
  selectedIndex = (int)textView.tag;
  [self performSelector:@selector(scrollTextViewToBottom:) withObject:textView afterDelay:0.01];

  return YES;
}

- (void)textViewDidBeginEditing:(UITextView *)textView {
  [self resizeTableView];
}

- (void)textViewDidChange:(UITextView *)textView {
  [testData replaceObjectAtIndex:textView.tag withObject:textView.text]  ;
  [self resizeTableView];
}


- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
  if([text isEqualToString:@"n"]) {
    [self.view endEditing:YES];
    selectedIndex = -1;
    [self resizeTableView];
    return NO;
  }
  return YES;
}

- (void)textViewDidEndEditing:(UITextView *)textView {
  textView.textContainer.maximumNumberOfLines = 2;
  textView.textContainer.lineBreakMode = NSLineBreakByTruncatingTail;
  textView.font = [UIFont openSansRegularFontWithSize:12];
  [testData replaceObjectAtIndex:textView.tag withObject:textView.text];
}

- (void)scrollToCursorForTextView: (UITextView*)textView {
  CGRect cursorRect = [textView caretRectForPosition:textView.selectedTextRange.start];
  cursorRect = [self.feedbackTableView convertRect:cursorRect fromView:textView];
  if (![self rectVisible:cursorRect]) {
    [self.feedbackTableView scrollRectToVisible:cursorRect animated:YES];
  }
}

- (BOOL)rectVisible: (CGRect)rect {
  CGRect visibleRect;
  visibleRect.origin = self.feedbackTableView.contentOffset;
  visibleRect.origin.y += self.feedbackTableView.contentInset.top;
  visibleRect.size = self.feedbackTableView.bounds.size;
  visibleRect.size.height -= self.feedbackTableView.contentInset.top +     self.feedbackTableView.contentInset.bottom;

  return CGRectContainsRect(visibleRect, rect);
}

-(void)scrollTextViewToBottom:(UITextView *)textView {
  [textView setSelectedRange:NSMakeRange([textView.text length], 0)];
  [textView setScrollEnabled:NO];
  [self scrollToCursorForTextView:textView];
}

- (void)resizeTableView {
  [self.feedbackTableView beginUpdates];
  [self.feedbackTableView endUpdates];
}

The problem that I am facing is that whenever I am changing characters in the textview the size of the cell changes and the gap between the textview and contentview bottom increases.

Any Idea how can I solve this.

Leave a Reply