UITextInputMode currentInputMode is deprecated. Suggested replacement?

梦想的初衷 提交于 2019-12-05 10:37:40

The object for the notification UITextInputCurrentInputModeDidChangeNotification is an instance of UITextInputMode.

UITextInputMode *currentInputMode = [notification object];

Also, you can get the active input mode on a particular responder:

UITextView *textView = [[UITextView alloc] init];
UITextInputMode *currentInputMode = textView.textInputMode;

Currently on iOS 7, the textInputMode/notification object are nil for the emoji keyboard. It's unclear if this is intended behaviour.

I don't think you should create new UITextView or UITextField.

You already have one if your app uses keyboard. Just get .textInputMode.primaryLanguage from your existing text control.

If you have several text views/field and don't know, which one is active, you can call isFirstResponder on each. But looks like you can get textInputMode from any text control and don't need to find active one.

The correct but stupid answer is: use textInputMode on a UITextField that is part of the view-hirarchy.

If you happen (like me) to not have one (eg because you implement an inputView and have no official way of finding the first responder) that might look like this:

UTextField* p = [[UITextField alloc] init];
p.hidden = YES;
[someView addSubview: p];
... = p.textInputMode.primaryLanguage;
[p removeFromSuperview];

That's so obvious and simple and only needs 3/4KB of heap. Compared to a few bytes of the code in the UIKit-Library. That really makes sense... :-)

user3735775
[UIApplication sharedApplication].delegate.window.textInputMode

It works on ios9. Not checked on earlier iOS versions.

Swift 2.0

//richT is the TextView or textField
var language = richT.textInputMode?.primaryLanguage
print("language: \(language)")
//UITextInputMode().primaryLanguage - returns nil value

Update your code like this:

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