How can I add support for Chinese keyboard with UITextView on iOS 7?

两盒软妹~` 提交于 2019-12-25 06:38:24

问题


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];
}

回答1:


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];
}


来源:https://stackoverflow.com/questions/24467333/how-can-i-add-support-for-chinese-keyboard-with-uitextview-on-ios-7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!