【问题标题】:Detecting current iPhone input language检测当前 iPhone 输入语言
【发布时间】:2010-11-27 17:51:15
【问题描述】:

有人知道,我可以在 iPhone 应用程序中获取当前的输入语言和/或键盘布局吗?输入语言更改时我也能收到通知吗?

【问题讨论】:

标签: iphone keyboard


【解决方案1】:

在 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 感兴趣。)

【讨论】:

  • 自 iOS7 起已弃用。
  • 因为这个方法在iOS7中被弃用了,最好使用当前第一响应者的方法textInputMode。它的工作原理相同。看我的回答。
【解决方案2】:

您可以通过 UIResponder 方法 textInputMode 询问当前的第一响应者(UITextField、UISearchBar 等):

// assume you have instance variable pointing to search bar currently entering
UITextInputMode *inputMode = [self.searchBar textInputMode];
NSString *lang = inputMode.primaryLanguage;

【讨论】:

    【解决方案3】:

    您可以将观察者添加到默认通知中心:

    [[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);
        }
    }
    

    但是:以上所有内容都没有记录,您不应该在运输代码中使用它!

    【讨论】:

    • 您可能得到了否定,因为您正在描述无法使用 SDK 发布的代码。
    • 有趣,但没用——如果我确实不能在“运输”代码中使用的话。
    • 我还是不明白... 3 次反对某些反向工程信息,清楚地表明它不适用于发布应用程序。 Downvoters,您是否被此答案中的信息冒犯了?
    • 我不赞成,因为 UITextInputMode 提供了一种支持的方式来做到这一点。
    • @Vincent Gable:请注意,此答案是在 UITextInputMode 尚不可用时编写的(iOS 4.2,2010 年 11 月发布)。
    【解决方案4】:

    来自 Apple 参考库 - “Getting the Current Language and Locale”:

    NSUserDefaults* defs = [NSUserDefaults standardUserDefaults];
    NSArray* languages = [defs objectForKey:@"AppleLanguages"];
    NSString* preferredLang = [languages objectAtIndex:0];
    

    【讨论】:

    • [NSLocale preferredLanguages] 是获取首选实验室参数列表的官方方式。但我认为@Danya 想要的是当前键盘布局的名称。那是另一回事。
    • 没错。我需要根据当前输入语言更改视图的内容。所以,我需要有一种方法来获取当前的键盘语言以及当用户按下键盘按钮更改语言时的通知。
    • 这很有帮助,因为 [NSLocale currentLocale] 不会为您提供用户的 UI 语言!
    • OP 要求输入键盘语言;这与它无关。也直接阅读AppleLanguages 偏好是一个坏主意;如果你真的想要用户的首选语言,你可以通过 NSLocale.preferredLanguages.firstObject 获得。请注意,这不意味着您的应用程序正在以这种语言运行,因为首选语言可能是您的应用程序不支持的语言。
    【解决方案5】:

    根据最佳答案,以下是在更改键盘语言时获取键盘语言的通用解决方案。注册通知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
    

    【讨论】:

      【解决方案6】:

      我的做法如下:

      • 将您的 ViewController 注册为UIApplicationDidBecomeActiveNotification 的侦听器

        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidBecomeActive:) name:UIApplicationDidBecomeActiveNotification object:nil];

      • 在 applicationDidBecomeActive 处理程序中,使用 [NSLocale preferredLanguages] 检查当前语言并采取相应措施。

      这种方法可以满足您的需求,并且完全可交付,无需使用私有 API。

      【讨论】:

      • 这将返回 UI 语言,而不是输入/键盘语言。
      猜你喜欢
      • 1970-01-01
      • 2012-10-08
      • 2023-04-02
      • 1970-01-01
      • 2011-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-01
      相关资源
      最近更新 更多