Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITextView, scroll while editing?

I've got a big UITextView, nearly full screen. If I tap it, the keyboard pops up and I can edit the text. However, the longer I type, eventually the text runs down the view, behind the keyboard. I can no longer see what I'm typing.

How do you deal with this? Do you have to track the cursor position and scroll the view manually?

like image 734
jamil Avatar asked May 05 '12 07:05

jamil


2 Answers

You need to use following code for scroll down textview according to text range(or say according to typing)

NSRange range = NSMakeRange(textView.text.length - 1, 1);
[textView scrollRangeToVisible:range];

Hope, this will help you...

like image 183
Nitin Avatar answered Nov 03 '22 02:11

Nitin


I guess you have to size your UITextView as keyboard shows/hides. So the keyboard won't be over your textview. Here is the sample codes.

- (void)viewDidLoad
{
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillHide:)
                                                 name:UIKeyboardWillHideNotification
                                               object:nil];
}

- (void)keyboardWillShow:(NSNotification *)notification
{
    [UIView beginAnimations:nil context:nil];
    CGRect endRect = [[notification.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGRect newRect = YOUT_TEXT_VIEW.frame;
    //Down size your text view
    newRect.size.height -= endRect.size.height;
    YOUT_TEXT_VIEW.frame = newRect;
    [UIView commitAnimations];
}

- (void)keyboardWillHide:(NSNotification *)notification
{
    ... // Resize your textview when keyboard is going to hide
}
like image 32
Thant Thet Avatar answered Nov 03 '22 01:11

Thant Thet