【发布时间】: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