-->

How to show specific language keyboard when user i

2019-01-23 21:53发布

问题:

I want to build an iPhone Application which supports multi-language. In that I want to input values in the language which user has selected from a set of languages in my application's settings. Accordingly, it should show selected language keyboard.

For eg : If user has selected French Language from my application's setting. Then in all the UITextFields of my application, user should be able to input values in French Language. So for that when keyboard opens I need to make sure that it is French Language Keyboard which it shows every time instead of default language keyboard.

So the question is, is It possible to show specific language keyboard whenever user taps on UITextField based on the language selected in the application's settings? If yes then how?

回答1:

You can't do this without change device languages whether it is from setting or via coding like this. //ex spanish language (es).

NSArray *langOrder = [NSArray arrayWithObjects:@"es", nil];
[[NSUserDefaults standardUserDefaults] setObject:langOrder forKey:@"AppleLanguages"];

this will just change language order..

you cannot control the keyboard according to your selected language. The user chooses which keyboards they would like available via the settings application and can toggle between them using the globe icon on the keyboard.



回答2:

You need to override textInputMode for your UITextView

@implementation UITextView (Localization)

- (UITextInputMode *) textInputMode {
    for (UITextInputMode *inputMode in [UITextInputMode activeInputModes])
    {
        if ([[self langFromLocale:[self localeKey]] isEqualToString:[self langFromLocale:inputMode.primaryLanguage]])
            return inputMode;
    }
    return [super textInputMode];
}


- (NSString*) localeKey
{
    NSArray* lang = [[NSUserDefaults standardUserDefaults]objectForKey:@"AppleLanguages"];
    NSString* currentLang = lang[0];
    return currentLang;
}

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

And you now can chan change input lang:

[[NSUserDefaults standardUserDefaults] setObject:@[@"fr"] forKey:@"AppleLanguages"];
[[NSUserDefaults standardUserDefaults] synchronize];


回答3:

I go throw the apple document Managing the keyboard there is no single word saying about what you want. and found below line

The input method and layout for the keyboard is determined by the user’s language preferences.