【问题标题】:How can I add support for Chinese keyboard with UITextView on iOS 7?如何在 iOS 7 上使用 UITextView 添加对中文键盘的支持?
【发布时间】:2014-08-19 11:30:06
【问题描述】:

如何在 iOS 7 上使用 UITextView 添加对中文键盘的支持?目前我正在使用以下代码。但它仅适用于标准尺寸的键盘。它只为主键盘调整UITextView的大小,没有额外的中文面板。

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

【问题讨论】:

  • 以下方法没有帮助:stackoverflow.com/questions/8540803/…(这是关于另一个问题)。
  • 您必须更具体地了解您的问题。你不需要做任何事情来支持任何键盘,这是自动的。您的代码所做的是根据键盘的大小调整 UITextView 的大小,并且由于任何地方都没有硬编码,我(还)不明白为什么它不适用于所有键盘大小?究竟是什么问题?我认为屏幕截图会有所帮助。
  • 它只为主键盘调整UITextView的大小,没有额外的中文面板。
  • 你需要告诉我们哪个中文键盘,并给我们截图,因为我尝试过很多中文键盘,但我没有看到一个带有主键盘和附加面板的键盘?
  • 对不起,我不是中国人,有9种不同的中文键盘,所以我花了一段时间才找到你在说什么。现在看到了。我最好的猜测(尚未测试)是 iOS 将在添加该位时发送新的keyboardWillShow 通知,由于if (!keyboardIsShown) 测试,这会在您的代码中中断。您可能需要处理已经显示键盘并再次收到通知的情况。该方法中的快速日志或断点将确认这一点。

标签: ios ios7 keyboard uitextview chinese-locale


【解决方案1】:

感谢 jcaron。正确代码:

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

【讨论】:

    猜你喜欢
    • 2013-10-14
    • 2023-04-04
    • 2019-12-24
    • 2011-12-17
    • 1970-01-01
    • 2011-12-31
    • 1970-01-01
    • 2011-10-17
    • 1970-01-01
    相关资源
    最近更新 更多