0

How can I add support for Chinese keyboard with UITextView on iOS 7? Currently I'm using the following code. But it works correctly only for standard-sized keyboards. It resizes UITextView only for the main keyboard without additional Chinese panel.

bool keyboardIsShown;
float keyboardDelta;

- (void)keyboardWillShow:(NSNotification*)aNotification {
    if (!keyboardIsShown) {
        keyboardIsShown = true;
        NSDictionary* userInfo = [aNotification userInfo];
        CGSize keyboardSize = [[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
        if (is_Landscape) {
            keyboardSize = CGSizeMake(keyboardSize.height, keyboardSize.width);
        }
        keyboardSize.height -= tabBarController.tabBar.frame.size.height;
        CGRect viewFrame = myUITextView.frame;
        keyboardDelta = keyboardSize.height;
        viewFrame.size.height -= keyboardDelta;

        NSTimeInterval animationDuration;
        UIViewAnimationCurve animationCurve;
        [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&animationCurve];
        [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&animationDuration];
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationCurve:animationCurve];
        [UIView setAnimationDuration:animationDuration];
        [myUITextView setFrame:viewFrame];
        [UIView commitAnimations];
    }
}

- (void)keyboardWillHide:(NSNotification*)aNotification {
    keyboardIsShown = false;
    CGRect viewFrame = editor.frame;
    viewFrame.size.height += keyboardDelta;

    NSDictionary* userInfo = [aNotification userInfo];
    NSTimeInterval animationDuration;
    UIViewAnimationCurve animationCurve;
    [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&animationCurve];
    [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&animationDuration];
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationCurve:animationCurve];
    [UIView setAnimationDuration:animationDuration];
    [myUITextView setFrame:viewFrame];
    [UIView commitAnimations];
}
Dmitry
  • 14,306
  • 23
  • 105
  • 189
  • The following approach doesn't help: http://stackoverflow.com/questions/8540803/how-to-get-keyboard-size-to-resize-uitextview-and-how-to-use-uikeyboardframeendu?rq=1 (it's about another issue). – Dmitry Jun 28 '14 at 13:40
  • You'll have to be more specific as to what your question is. You don't need to do anything to support any keyboard, this is automatic. What your code does is resize your UITextView based on the size of the keyboard, and as there isn't anything hardcoded anywhere, I don't (yet) see why it would not work for all keyboard sizes? What exactly is the problem? I think screenshots would help. – jcaron Jun 28 '14 at 13:57
  • It resizes `UITextView` only for the main keyboard without additional Chinese panel. – Dmitry Jun 28 '14 at 14:27
  • You'll need to tell us which Chinese keyboard, and show us a screenshot, because I've tried many Chinese keyboards, and I don't see one with a main keyboard and and additional panel? – jcaron Jun 28 '14 at 14:32
  • jcaron, you should type something to see additional panel. It's basic iOS functionality for Chinese users. – Dmitry Jun 28 '14 at 14:34
  • 1
    Sorry, I'm not Chinese, and there are 9 different Chinese keyboards, so it took me a while to find what you are talking about. Now seen it. My best guess (haven't tested) is that iOS will send a new `keyboardWillShow` notification when that bit is added, which breaks in your code because of the `if (!keyboardIsShown)` test. You probably need to handle the case where the keyboard is already shown and you receive the notification again. A quick log or breakpoint in that method will confirm this. – jcaron Jun 28 '14 at 14:41

1 Answers1

0

Thanks to jcaron. The correct code:

bool keyboardIsShown;
float editorHeight;

- (void)keyboardWillShow:(NSNotification*)aNotification {
    NSDictionary* userInfo = [aNotification userInfo];
    CGSize keyboardSize = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
    if (is_Landscape) {
        keyboardSize = CGSizeMake(keyboardSize.height, keyboardSize.width);
    }
    keyboardSize.height -= tabBarController.tabBar.frame.size.height;
    CGRect viewFrame = editor.frame;
    if (!keyboardIsShown) {
        editorHeight = viewFrame.size.height;
    }
    viewFrame.size.height = editorHeight - keyboardSize.height;
    keyboardIsShown = true;

    NSTimeInterval animationDuration;
    UIViewAnimationCurve animationCurve;
    [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&animationCurve];
    [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&animationDuration];
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationCurve:animationCurve];
    [UIView setAnimationDuration:animationDuration];
    [editor setFrame:viewFrame];
    [UIView commitAnimations];
}

- (void)keyboardWillHide:(NSNotification*)aNotification {
    keyboardIsShown = false;
    CGRect viewFrame = editor.frame;
    viewFrame.size.height = editorHeight;

    NSDictionary* userInfo = [aNotification userInfo];
    NSTimeInterval animationDuration;
    UIViewAnimationCurve animationCurve;
    [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&animationCurve];
    [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&animationDuration];
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationCurve:animationCurve];
    [UIView setAnimationDuration:animationDuration];
    [editor setFrame:viewFrame];
    [UIView commitAnimations];
}
Dmitry
  • 14,306
  • 23
  • 105
  • 189