问题描述
我已经找到了很多解决方法,但是没有可行的解决方案.这是我尝试但没有奏效的方法.
I already found a lot of approaches for this but no working solution. Here's what I tried and didn't work.
(1)只需调用 primaryLanguage()
UITextInputMode().primaryLanguage
→总是返回nil:-/
→ always returns nil :-/
(2)订阅 UITextInputCurrentInputModeDidChangeNotification 通知
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)使用 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.
如何获取当前活动键盘的NSLocale?
How do I get the NSLocale of the currently active keyboard?
推荐答案
您可以通过访问以下文本字段 textInputMode
来从每个文本字段访问primaryLanguage:
You can access the primaryLanguage from every textfield by accessing the textfields textInputMode
like that:
var language = textfield.textInputMode?.primaryLanguage
这篇关于将当前活动的键盘语言获取为NSLocale的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!