Getting the currently active keyboard language as NSLocale

吃可爱长大的小学妹 提交于 2021-02-17 06:10:19

问题


I already found a lot of approaches for this but no working solution. Here's what I tried and didn't work.

(1) Simply calling primaryLanguage()

UITextInputMode().primaryLanguage

→ always returns nil :-/

(2) Subscribing to UITextInputCurrentInputModeDidChangeNotification notifications

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

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

The notification is getting triggered but it is unclear how I can extract the current language information from the notification.

(3) Using activeInputModes()

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

This always provides the same array of all available keyboards, no information on the actually active one.

How do I get the NSLocale of the currently active keyboard?


回答1:


You can access the primaryLanguage from every textfield by accessing the textfields textInputMode like that:

var language = textfield.textInputMode?.primaryLanguage


来源:https://stackoverflow.com/questions/28682576/getting-the-currently-active-keyboard-language-as-nslocale

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!