UIKeyboard打开大写锁定 [英] UIKeyboard turn Caps Lock on

查看:60
本文介绍了UIKeyboard打开大写锁定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要我的用户输入一些数据,例如DF-DJSL,所以我将其放在代码中:

I need my user to input some data like DF-DJSL so I put this in the code:

theTextField.autocapitalizationType = UITextAutocapitalizationTypeAllCharacters;

但是不幸的是,CAPS中的第一个字母类型会发生这种情况,但是键入连字符后立即将字母转换为小写字母,然后其余字符返回到CAPS,因此会产生这样的输出(除非用户在之后手动点按Shift键输入连字符):DF-dJSL

But unfortunately what happens is the first to letter type in CAPS but then letter immediately after typing the hyphen will be in lower case and then the rest return to CAPS therefore producing output like this (unless the user manually taps the shift button after typing a hyphen): DF-dJSL

我该如何解决?

非常感谢

推荐答案

您没有提到您使用的是哪个SDK,但是在3.0及更高版本中,我看到了您想要的行为.

You don't mention which SDK you're using, but against 3.0 and above I see your desired behaviour.

也就是说,当使用委托中的 textFieldDidEndEditing 方法完成编辑时,您始终可以将文本更改为大写:

That said, you could always change the text to upper case when they finish editing using the textFieldDidEndEditing method from the delegate:

- (void)textFieldDidEndEditing:(UITextField *)textField {
    NSString *textToUpper = [textField.text uppercaseString];   
    [theTextField setText:textToUpper];
}

或者,通过在文本字段更改时在其上设置通知,您可以在键入文本时更改该文本:

Or, by setting up a notification on the textfield when it changes, you could change the text as it is being typed:

// setup the UITextField
{
    theTextField.delegate = self;
    theTextField.autocapitalizationType = UITextAutocapitalizationTypeAllCharacters;
    [theTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
}

您必须这样做,因为与 UISearchBar 不同, UITextField 不实现 textDidChange .也许是这样的吗?

You have to do it this way since, unlike UISearchBar, UITextField doesn't implement textDidChange. Something like this, perhaps?

- (void)textFieldDidChange:(UITextField *)textField {
    NSRange range = [textField.text rangeOfString : @"-"];
    if (range.location != NSNotFound) {
        theTextField.autocapitalizationType = UITextAutocapitalizationTypeAllCharacters;
    }
}

这篇关于UIKeyboard打开大写锁定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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