【问题标题】:How to get the keyboard locale of uitextfield如何获取 uitextfield 的键盘语言环境
【发布时间】:2016-06-21 16:15:14
【问题描述】:

我在视图控制器上有一个 uitextfield 对象。用户可以使用任何键盘语言输入任何文本。

问题是:如何通过用户选择的键盘获取 uitextfield 中输入文本的语言 id?

【问题讨论】:

    标签: objective-c cocoa-touch uitextfield


    【解决方案1】:

    iOS7.0以上

    - (void)textFieldDidEndEditing:(UITextField *)textField {
    NSString *primaryLanguageId = textField.textInputMode.primaryLanguage;
    }
    

    iOS7.0及以下

    - (void)textFieldDidEndEditing:(UITextField *)textField {
    NSString *primaryLanguageId = [UITextInputMode currentInputMode].primaryLanguage;
    }
    

    【讨论】:

      【解决方案2】:

      您必须使用[UITextInputMode currentInputMode].primaryLanguage 获取语言代码字符串,但您还必须添加观察者,因为如果键盘语言是 chane,则此观察者会通知语言已更改。

      Objective-C

      - (void)viewDidLoad
      {
          [super viewDidLoad];
          [[NSNotificationCenter defaultCenter] addObserver:self
                                                   selector:@selector(changeInputMode:)
                                                       name:UITextInputCurrentInputModeDidChangeNotification object:nil];}
      }
      
      //This method prints the currently selected input language (like "en_US" or "de_DE"):
      
      -(void)changeInputMode:(NSNotification *)notification
      {
          NSString *inputMethod = [[UITextInputMode currentInputMode] primaryLanguage];
          NSLog(@"inputMethod=%@",inputMethod);
      }
      

      斯威夫特:

      class ViewController: UIViewController
      {
      
          override func viewDidLoad() {
              super.viewDidLoad()
      
              NSNotificationCenter.defaultCenter().addObserver(self,
                                                               selector: "changeInputMode:",
                                                               name: UITextInputCurrentInputModeDidChangeNotification, object: nil)
          }
      
          func changeInputMode(notification : NSNotification)
          {
              let inputMethod = UITextInputMode.currentInputMode().primaryLanguage
              println("inputMethod: \(inputMethod)")
          }
      
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-01-27
        • 1970-01-01
        • 1970-01-01
        • 2011-08-15
        • 1970-01-01
        • 1970-01-01
        • 2019-12-17
        • 1970-01-01
        相关资源
        最近更新 更多