Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a UITextView in a custom UITableView. I'm trying to make the height dynamically size itself, and scroll to the correct location (cursors location).

Objective-C
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if ([text isEqualToString:@"\n"])
    {
        NSRange cursor = NSMakeRange(range.location + 1, 0);

        NSMutableString *mutableT = [NSMutableString stringWithString:textView.text];
        [mutableT insertString:@"\n " atIndex:range.location];
        [textView setText:mutableT];

        [textView setSelectedRange:cursor];
        [textView sizeToFit];
        [textView layoutIfNeeded];

        [self.myTableView beginUpdates];
        [self.myTableView endUpdates];
        return NO;
    }
    return YES;
}


First:, when the text has to go to the next line, without the user enter a new line (word wrap), then this whole code doesn't work.

Second: When the user enters a new line, and it gets out of view, the tableView gets scrolled to the top. Then when I enter another char, it gets back to normal.
Posted

1 solution

I would disable autoscrolling on the textview.

This code should do the job:

Objective-C
[textView scrollRangeToVisible:[textView selectedRange]];
 
Share this answer
 
Comments
Member 11360293 18-Mar-15 11:44am    
Tried that. What happens is, the tableview scrolls all the way up, then when I enter a new character, it goes back to normal

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900