Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone: Change Keyboard language programmatically

I am trying to provide a different language support on my iOS 5.x application whenever native Keyboard is opened. Provide this language in native keyboard programmatically. Could someone guide me how can i support it? I saw a carbon framework, but looks like its for Mac apps.

Thanks.

like image 703
Getsy Avatar asked Sep 26 '12 06:09

Getsy


People also ask

How do I change the keyboard language in IOS Swift?

Open Microsoft SwiftKey. Tap 'Languages' Ensure that both of your enabled languages have the a different layout selected (i.e QWERTY & AZERTY) Press and hold the spacebar and slide to choose your desired layout.

How do I make English my default keyboard on my iPhone?

Tap and hold the Globe icon in the bottom-left corner of the screen. (If you're using an older iPhone with a Home button or an iPad, the Globe icon will show up in the last row of the keyboard.) You'll now see a list of all available keyboards. Spot the newly added language keyboard and then tap it to switch languages.


2 Answers

You can do it starting from iOS 7 on a per UIResponder basis. There is textInputMode property in UIResponder class. It is readonly, but the documentation says:

The text input mode identifies the language and keyboard displayed when this responder is active.

For responders, the system normally displays a keyboard that is based on the user’s language preferences. You can redefine this property and use it to return a different text input mode in cases where you want a responder to use a specific keyboard. The user can still change the keyboard while the responder is active, but switching away to another responder and then back restores the keyboard you specified.

In my project I created a subclass of UITextField and defined a new property called userDefinedKeyboardLanguage. I also overrode above mentioned textInputMode method. It looks similar to the following:

- (UITextInputMode *) textInputMode {     for (UITextInputMode *tim in [UITextInputMode activeInputModes]) {         if ([[Utilities langFromLocale:userDefinedKeyboardLanguage] isEqualToString:[Utilities langFromLocale:tim.primaryLanguage]]) return tim;     }     return [super textInputMode]; } 

I have also a custom method +(NSString *)langFromLocale:(NSString *)locale in my Utilities class which looks like this:

+ (NSString *)langFromLocale:(NSString *)locale {     NSRange r = [locale rangeOfString:@"_"];     if (r.length == 0) r.location = locale.length;     NSRange r2 = [locale rangeOfString:@"-"];     if (r2.length == 0) r2.location = locale.length;     return [[locale substringToIndex:MIN(r.location, r2.location)] lowercaseString]; } 

Now my custom textfield class can change the keyboard input language simply by setting userDefinedKeyboardLanguage property to the desired language.

like image 123
Dima Rybachenko Avatar answered Sep 19 '22 19:09

Dima Rybachenko


I know it is old question, but here it is my way to change keyboard language.

Thank to @the4kman for the mark: this way can change current keyboard only to those which were added in Settings.

Swift 3:

class CustomTextField: UITextField {      private func getKeyboardLanguage() -> String? {         return "en" // here you can choose keyboard any way you need     }      override var textInputMode: UITextInputMode? {         if let language = getKeyboardLanguage() {             for tim in UITextInputMode.activeInputModes {                 if tim.primaryLanguage!.contains(language) {                     return tim                 }             }         }         return super.textInputMode     }  } 
like image 32
kotvaska Avatar answered Sep 18 '22 19:09

kotvaska