如何检测用户何时更换键盘? [英] How to detect when user changes keyboards?
问题描述
有什么方法可以检测用户何时更改键盘类型,在这种情况下特别是表情符号键盘?
Is there some way to detect when the user changes keyboard types, specifically to the Emoji keyboard in this case?
推荐答案
您可以使用 UITextInputMode
来检测 currentInputMode
的当前语言-表情符号被视为一种语言。从文档:
You can use UITextInputMode
to detect the current language of the currentInputMode
-- emoji is considered a language. From the docs:
UITextInputMode
类的实例表示当前的
文本输入模式。您可以使用此对象来确定当前用于文本输入的主要
语言。
An instance of the
UITextInputMode
class represents the current text-input mode. You can use this object to determine the primary language currently being used for text input.
您可以测试表情符号像这样的键盘:
You can test for the emoji keyboard like this:
NSString *language = [[UITextInputMode currentInputMode] primaryLanguage];
BOOL isEmoji = [language isEqualToString:@"emoji"];
if (isEmoji)
{
// do something
}
可以通过 UITextInputCurrentInputModeDidChangeNotification
通知输入模式更改。这将在当前输入模式更改时发布。
You can be notified of the input mode changing via the UITextInputCurrentInputModeDidChangeNotification
. This will post when the current input mode changes.
这是一个简单的应用程序,只要更改模式,该应用程序就会打印 NSLog
:
Here's a simple app which prints an NSLog
whenever the mode changes:
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(changeInputMode:)
name:UITextInputCurrentInputModeDidChangeNotification object:nil];}
}
-(void)changeInputMode:(NSNotification *)notification
{
NSString *inputMethod = [[UITextInputMode currentInputMode] primaryLanguage];
NSLog(@"inputMethod=%@",inputMethod);
}
或者,如果您更喜欢 Swift :
import UIKit
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)")
}
}
这篇关于如何检测用户何时更换键盘?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!