当用户在 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 中输入值时,如何显示特定语言键盘?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

No resource found that matches the given name: attr #39;android:keyboardNavigationCluster#39;. when updating to Support Library 26.0.0(找不到与给定名称匹配的资源:attr android:keyboardNavigationCluster.更新到支持库 26.0.0 时) - IT屋-程序员软
How to resize UITextView on iOS when a keyboard appears?(出现键盘时如何在iOS上调整UITextView的大小?)
How to reliably detect if an external keyboard is connected on iOS 9?(如何可靠地检测 iOS 9 上是否连接了外部键盘?)
How to mimic Keyboard animation on iOS 7 to add quot;Donequot; button to numeric keyboard?(如何在 iOS 7 上模拟键盘动画以添加“完成数字键盘的按钮?)
How do I dismiss the iOS keyboard?(如何关闭 iOS 键盘?)
Is possible to simulate touch event using an external keyboard on ios jailbroken?(是否可以在 ios 越狱后使用外部键盘模拟触摸事件?)