How to show specific language keyboard when user input values in UITextField in iPhone App?(当用户在 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 中输入值时,如何
基础教程推荐
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01