将当前活动的键盘语言作为NSLocale获取

时间:2015-02-23 20:13:57

标签: ios swift uitextinput

我已经找到了很多方法,但没有可行的解决方案。这是我尝试过但没有用的东西。

(1)只需拨打 primaryLanguage()

即可
UITextInputMode().primaryLanguage

→始终返回nil: - /

(2)订阅 UITextInputCurrentInputModeDidChangeNotification 通知

override func viewDidLoad() {
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "changeInputMode:", name: UITextInputCurrentInputModeDidChangeNotification, object: nil)
}

func changeInputMode(sender : NSNotification) {
    ...?!
}

通知已被触发,但我不知道如何从通知中提取当前语言信息。

(3)使用 activeInputModes()

let localeIdentifier = UITextInputMode.activeInputModes().first as? UITextInputMode
var locale:NSLocale = NSLocale(localeIdentifier: localeIdentifier.primaryLanguage!)
println("Input Mode Language \(localeIdentifier.primaryLanguage!)")

这始终提供所有可用键盘的相同数组,没有关于实际活动键盘的信息。

如何获取当前活动键盘的NSLocale?

1 个答案:

答案 0 :(得分:6)

您可以通过访问文本字段textInputMode来访问每个文本字段中的primaryLanguage:

var language = textfield.textInputMode?.primaryLanguage
相关问题