iOS是否通过代码将默认键盘从ABC模式切换到123模式? [英] iOS toggle default keyboard from ABC mode to 123 mode via code?

查看:299
本文介绍了iOS是否通过代码将默认键盘从ABC模式切换到123模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以通过以下方法了解如何设置键盘的整体类型:

I can see how to set the keyboard's overall type via:

self.myTextView.keyboardType = UIKeyboardTypeDefault;

如何将默认键盘的模式从ABC切换到123,然后通过代码再次切换回原来的模式?基本上,当用户点击@字符时(当处于123模式时,@符号可用),我想将其键盘切换回ABC模式.

How can I toggle the default keyboard's mode from ABC to 123 and back again via code? Basically the moment the user taps the @ character (the @ symbol is available when they're in 123 mode) I want to switch their keyboard back to ABC mode.

有什么想法吗?

推荐答案

您也许可以通过使用UITextViewDelegate来完成此任务.它允许您拦截在UITextView中按下的键.这将允许您在用户按下某个键时切换键盘.为了恢复为UIKeyboardTypeDefault键盘的默认状态,您需要将键盘类型更改为另一种,然后再恢复为默认状态.

You might be able to accomplish this in by using the UITextViewDelegate. It allows you to intercept the keys as they are pressed in the UITextView. This will allow you to switch keyboards when the user presses a certain key. In order to revert back to the default state of the UIKeyboardTypeDefault keyboard, you'll need to change the keyboard type to another, then back to the default.

在ViewController.h文件中,使其实现UITextViewDelegate协议:

In your ViewController.h file, make it implement the UITextViewDelegate protocol:

@interface ViewController : UIViewController <UITextViewDelegate>

在ViewController.m的ViewController的viewDidLoad方法中,将textField的委托设置为视图控制器:

In your ViewController's viewDidLoad method in the ViewController.m, set the textField's delegate to the view controller:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    self.myTextView.delegate = self;
}

最后,我们需要捕获正在输入的键,并适当地更改键盘.我们在shouldChangeCharactersInRange:方法中进行此操作.

Finally, we need to capture the key as it is being entered and change the keyboard appropriately. We do this in the shouldChangeCharactersInRange: method.

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if( [@"@" isEqualToString:text] )
    {
        NSLog( @"Toggling keyboard type");
        textView.inputView = nil;
        [textView resignFirstResponder];
        [textView setKeyboardType:UIKeyboardTypeEmailAddress];
        [textView becomeFirstResponder];
        [textView reloadInputViews];

        textView.inputView = nil;
        [textView resignFirstResponder];
        [textView setKeyboardType:UIKeyboardTypeDefault];
        [textView becomeFirstResponder];
        [textView reloadInputViews];

    }
    return YES;
}

所以我最初很困惑,以为您实际上想更改键盘类型.现在,我正在阅读您的评论,我意识到您实际上是想将键盘重设为显示字母而不是数字和特殊字符的默认状态.上面的代码现在可以做到这一点.

So I was originally confused and thought you actually wanted to change the keyboard type. Now that I'm reading your comment, I realize that you're actually wanting to reset the keyboard to it's default state of showing the letters, instead of numbers and special characters. The above code now does that.

这篇关于iOS是否通过代码将默认键盘从ABC模式切换到123模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆