【问题标题】:Getting the currently active keyboard language as NSLocale获取当前活动的键盘语言作为 NSLocale
【发布时间】:2015-02-23 20:13:57
【问题描述】:

我已经找到了很多方法,但没有有效的解决方案。这是我尝试过但没有奏效的方法。

(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?

【问题讨论】:

    标签: ios swift uitextinput


    【解决方案1】:

    您可以像这样访问文本字段textInputMode,从每个文本字段访问primaryLanguage:

    var language = textfield.textInputMode?.primaryLanguage
    

    【讨论】:

    • 太好了,这行得通。唯一的缺点是它只在 textview 或 textfield 激活后才返回一个值——否则它是 nil。在 viewDidLoad 之后甚至在显示它之前也能获得活动的键盘语言会很好。
    • 在文本字段激活之前,键盘不存在。
    猜你喜欢
    • 2014-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-24
    • 2021-10-21
    • 1970-01-01
    • 2014-10-23
    相关资源
    最近更新 更多