gpt4 book ai didi

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

转载 作者:技术小花猫 更新时间:2023-10-29 10:33:41 25 4
gpt4 key购买 nike

有什么方法可以检测用户何时更改键盘类型,特别是在这种情况下更改为 Emoji 键盘?

最佳答案

您可以使用 UITextInputMode 来检测 currentInputMode 的当前语言——表情符号被视为一种语言。来自docs :

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.

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

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

您可以通过 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);
}

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


}

关于ios - 如何检测用户何时更改键盘?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24167856/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com