当用户在 iPhone App 的 UITextField 中输入值时,如何

How to show specific language keyboard when user input values in UITextField in iPhone App?(当用户在 iPhone App 的 UITextField 中输入值时,如何显示特定语言键盘?)

本文介绍了当用户在 iPhone App 的 UITextField 中输入值时,如何显示特定语言键盘?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想构建一个支持多语言的 iPhone 应用程序.在那里,我想以用户从我的应用程序设置中的一组语言中选择的语言输入值.因此,它应该显示选定的语言键盘.

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.

例如:如果用户从我的应用程序设置中选择了法语.然后在我的应用程序的所有 UITextFields 中,用户应该能够用法语输入值.因此,当键盘打开时,我需要确保它是每次显示的法语键盘,而不是默认语言键盘.

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.

所以问题是,是否可以根据应用程序设置中选择的语言在用户点击 UITextField 时显示特定语言键盘?如果是那怎么办?

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?

推荐答案

你需要为你的 UITextView 重写 textInputMode

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

这篇关于当用户在 iPhone App 的 UITextField 中输入值时,如何显示特定语言键盘?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:当用户在 iPhone App 的 UITextField 中输入值时,如何

基础教程推荐