How to show specific language keyboard when user input values in UITextField in iPhone App?

流过昼夜 提交于 2019-11-29 02:43:48

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.

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

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.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!