How to show specific language keyboard when user i

2019-01-23 21:19发布

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?

3条回答
劫难
2楼-- · 2019-01-23 21:44

I go throw the apple document Managing the keyboard there is no single word saying about what you want. and found below line
enter image description here
The input method and layout for the keyboard is determined by the user’s language preferences.

查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-01-23 21:51

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];
查看更多
做自己的国王
4楼-- · 2019-01-23 21:59

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.

查看更多
登录 后发表回答