Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get keyboard size to resize UITextView and how to use UIKeyboardFrameEndUserInfoKey with Japanese keyboard?

How to get a keyboard size to resize UITextView and how to use UIKeyboardFrameEndUserInfoKey with Japanese keyboard? The following code to resize UITextView works good on a standard keyboard. But doesn't work with Japanese. How to fix it?

- (void)keyboardWillShow:(NSNotification *)aNotification {
    [self moveTextViewForKeyboard:aNotification up:YES];
}

- (void)keyboardWillHide:(NSNotification *)aNotification {
    [self moveTextViewForKeyboard:aNotification up:NO]; 
}

- (void) moveTextViewForKeyboard:(NSNotification*)aNotification up: (BOOL) up
{
    NSDictionary* userInfo = [aNotification userInfo];

    // Get animation info from userInfo
    NSTimeInterval animationDuration;
    UIViewAnimationCurve animationCurve;

    CGRect keyboardEndFrame;

    [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&animationCurve];
    [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&animationDuration];
    [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardEndFrame];

    // Animate up or down
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:animationDuration];
    [UIView setAnimationCurve:animationCurve];

    CGRect newFrame = textView.frame;
    CGRect keyboardFrame = [self.view convertRect:keyboardEndFrame toView:nil];

    newFrame.size.height -= keyboardFrame.size.height * (up? 1 : -1);
    textView.frame = newFrame;

    [UIView commitAnimations];
}

Thanks a lot for the help!

like image 505
Dmitry Avatar asked Dec 16 '11 22:12

Dmitry


1 Answers

The correct fragment of the code:

CGRect newFrame = editSource.frame;
CGRect keyboardFrame = [self.view convertRect:keyboardEndFrame toView:nil];
keyboardFrame.size.height -= tabBarController.tabBar.frame.size.height;
if (up) {
    editHeight = newFrame.size.height;
    newFrame.size.height -= keyboardFrame.size.height;
} else {
    newFrame.size.height = editHeight;
}
editSource.frame = newFrame;

WARNIGN!

The method is obsolete. The correct answer is located here: How can I add support for Chinese keyboard with UITextView on iOS 7?

like image 117
Dmitry Avatar answered Oct 23 '22 03:10

Dmitry