【问题标题】:How to show specific language keyboard when user input values in UITextField in iPhone App?当用户在 iPhone App 的 UITextField 中输入值时,如何显示特定语言键盘?
【发布时间】:2015-04-03 18:43:15
【问题描述】:

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

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

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

【问题讨论】:

    标签: ios iphone keyboard uitextfield multilingual


    【解决方案1】:

    如果不更改设备语言,无论是通过设置还是通过这样的编码,您都无法做到这一点。 //ex 西班牙语(es)。

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

    这只会改变语言顺序..

    您无法根据您选择的语言控制键盘。用户可以通过设置应用程序选择他们希望使用的键盘,并可以使用键盘上的地球图标在它们之间切换。

    【讨论】:

    【解决方案2】:

    你需要为你的 UITextView 覆盖 textInputMode

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

    您现在可以更改输入语言:

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

    【讨论】:

    • 我的问题是,是否可以通过编码直接打开例如法语键盘而不是默认的英语键盘,同时点击 UITextField?
    【解决方案3】:

    我去扔苹果文件Managing the keyboard 没有一个词能说明你想要什么。并在以下行找到

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

    【讨论】:

    • 我的问题是,是否可以通过编码直接打开例如法语键盘而不是默认的英语键盘,同时点击 UITextField?
    猜你喜欢
    • 2013-01-05
    • 2011-03-03
    • 2015-05-22
    • 2011-02-14
    • 1970-01-01
    • 2010-12-24
    • 1970-01-01
    • 1970-01-01
    • 2011-09-29
    相关资源
    最近更新 更多