如何获取uitextfield的键盘区域设置

时间:2016-03-07 22:14:02

标签: objective-c cocoa-touch uitextfield

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

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

2 个答案:

答案 0 :(得分:0)

iOS7.0以上

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

iOS7.0及以下

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

答案 1 :(得分:0)

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

<强>目标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)")
    }


}