如何检测用户何时更改键盘?

Apo*_*llo 13 cocoa-touch uikeyboard ios emoji

有没有办法检测用户何时更改键盘类型,特别是在这种情况下更改为表情符号键盘?

mem*_*ons 20

您可以使用UITextInputMode检测当前语言currentInputMode- 表情符号被认为是一种语言.来自文档:

UITextInputMode类的实例表示当前的文本输入模式.您可以使用此对象来确定当前用于文本输入的主要语​​言.

您可以像这样测试表情符号键盘:

NSString *language = [[UITextInputMode currentInputMode] primaryLanguage];
BOOL isEmoji = [language isEqualToString:@"emoji"];
if (isEmoji)
{
   // do something
}
Run Code Online (Sandbox Code Playgroud)

您可以通过以下方式通知输入模式更改UITextInputCurrentInputModeDidChangeNotification.这将在当前输入模式改变时发布.

这是一个简单的应用程序,可以NSLog在模式更改时打印:

- (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);
}  
Run Code Online (Sandbox Code Playgroud)

或者如果您更喜欢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)")
    }


}
Run Code Online (Sandbox Code Playgroud)

  • 这也有效:myTextView.textInputMode.primaryLanguage; (3认同)
  • 注意:这不适用于ABC/123按钮.还在寻找一种方法来检查. (2认同)