【发布时间】:2010-11-27 17:51:15
【问题描述】:
有人知道,我可以在 iPhone 应用程序中获取当前的输入语言和/或键盘布局吗?输入语言更改时我也能收到通知吗?
【问题讨论】:
有人知道,我可以在 iPhone 应用程序中获取当前的输入语言和/或键盘布局吗?输入语言更改时我也能收到通知吗?
【问题讨论】:
在 iOS 4.2 及更高版本中,您可以使用 @987654321@ 类来确定当前用于文本输入的主要语言。
[UITextInputMode currentInputMode].primaryLanguage 将为您提供一个 NSString,代表 BCP 47 语言代码,例如“es”、“en-US”或“fr-CA”。
您可以注册UITextInputCurrentInputModeDidChangeNotification,以便在当前输入模式更改时收到警报。
(您可能还对"Getting Your Apps Ready for China and other Hot New Markets" WWDC 会议和Internationalization Programming Topics 感兴趣。)
【讨论】:
您可以通过 UIResponder 方法 textInputMode 询问当前的第一响应者(UITextField、UISearchBar 等):
// assume you have instance variable pointing to search bar currently entering
UITextInputMode *inputMode = [self.searchBar textInputMode];
NSString *lang = inputMode.primaryLanguage;
【讨论】:
您可以将观察者添加到默认通知中心:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(inputModeDidChange:)
name:@"UIKeyboardCurrentInputModeDidChangeNotification"
object:nil];
此方法打印当前选择的输入语言(如"en_US" 或"de_DE"):
- (void)inputModeDidChange:(NSNotification*)notification
{
id obj = [notification object];
if ([obj respondsToSelector:@selector(inputModeLastUsedPreference)]) {
id mode = [obj performSelector:@selector(inputModeLastUsedPreference)];
NSLog(@"mode: %@", mode);
}
}
但是:以上所有内容都没有记录,您不应该在运输代码中使用它!
【讨论】:
UITextInputMode 尚不可用时编写的(iOS 4.2,2010 年 11 月发布)。
来自 Apple 参考库 - “Getting the Current Language and Locale”:
NSUserDefaults* defs = [NSUserDefaults standardUserDefaults];
NSArray* languages = [defs objectForKey:@"AppleLanguages"];
NSString* preferredLang = [languages objectAtIndex:0];
【讨论】:
[NSLocale preferredLanguages] 是获取首选实验室参数列表的官方方式。但我认为@Danya 想要的是当前键盘布局的名称。那是另一回事。
AppleLanguages 偏好是一个坏主意;如果你真的想要用户的首选语言,你可以通过 NSLocale.preferredLanguages.firstObject 获得。请注意,这不意味着您的应用程序正在以这种语言运行,因为首选语言可能是您的应用程序不支持的语言。
根据最佳答案,以下是在更改键盘语言时获取键盘语言的通用解决方案。注册通知UITextInputCurrentInputModeDidChangeNotification:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(inputModeDidChange:) name:UITextInputCurrentInputModeDidChangeNotification object:nil];
然后在inputModeDidChange
-(void)inputModeDidChange:(NSNotification *)notification {
UIView *firstResponder = [UIView currentFirstResponder];
UITextInputMode *currentInputMode = firstResponder.textInputMode;
NSString *keyboardLanguage = [currentInputMode primaryLanguage];
NSLog(@"%@", keyboardLanguage); // e.g. en-US
}
currentFirstResponder 来自 UIView 上的类别以获得第一响应者视图,如this SO 帖子中所建议:
// UIView+Additions.h
#import <UIKit/UIKit.h>
@interface UIView (Additions)
+ (id)currentFirstResponder;
@end
实施
// UIView+Additions.m
#import "UIView+Additions.h"
static __weak id currentFirstResponder;
@implementation UIView (Additions)
+ (id)currentFirstResponder {
currentFirstResponder = nil;
// This will invoke on first responder when target is nil
[[UIApplication sharedApplication] sendAction:@selector(findFirstResponder:)
to:nil
from:nil
forEvent:nil];
return currentFirstResponder;
}
- (void)findFirstResponder:(id)sender {
// First responder will set the static variable to itself
currentFirstResponder = self;
}
@end
【讨论】:
我的做法如下:
将您的 ViewController 注册为UIApplicationDidBecomeActiveNotification 的侦听器
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidBecomeActive:) name:UIApplicationDidBecomeActiveNotification object:nil];
在 applicationDidBecomeActive 处理程序中,使用 [NSLocale preferredLanguages] 检查当前语言并采取相应措施。
这种方法可以满足您的需求,并且完全可交付,无需使用私有 API。
【讨论】: