iPhone强制文本框输入到大写

hun*_*m06 37 iphone cocoa-touch uitextfield uppercase

如何强制在iPhone上的文本框中输入字符为大写?

Ada*_*Woś 50

设置autocapitalizationTypeUITextAutocapitalizationTypeAllCharactersUITextField.

有关更多详细信息,请参阅UITextInputTraits协议(由UITextField采用).

  • 当然它没有,`string`只作为参数传递.你必须做一些像`textField.text = [textField.text stringByReplacingCharactersInRange:range withString:[string uppercaseString]]; 返回NO;` (15认同)

Mon*_*art 36

这种解决方案并不完全令人满意.

即使autocapitalizationType设置为UITextAutocapitalizationTypeAllCharacters,用户仍然可以按下盖子以释放大写锁定.并且textField.text = [textField.text stringByReplacingCharactersInRange:range withString:[string uppercaseString]]; return NO;解决方案并不是那么好:如果用户编辑文本的中间部分(在textField.text =编辑光标到达字符串的末尾之后),我们就会松开编辑点.

我已经完成了两个解决方案的混合,这就是我的建议:设置UITextAutocapitalizationTypeAllCharacters,并将以下代码添加到委托UITextField.

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range
replacementString:(NSString *)string {

    // Check if the added string contains lowercase characters.
    // If so, those characters are replaced by uppercase characters.
    // But this has the effect of losing the editing point
    // (only when trying to edit with lowercase characters),
    // because the text of the UITextField is modified.
    // That is why we only replace the text when this is really needed.
    NSRange lowercaseCharRange;
    lowercaseCharRange = [string rangeOfCharacterFromSet:[NSCharacterSet lowercaseLetterCharacterSet]];

    if (lowercaseCharRange.location != NSNotFound) {

        textField.text = [textField.text stringByReplacingCharactersInRange:range
                                                                 withString:[string uppercaseString]];
        return NO;
    }

    return YES;
}
Run Code Online (Sandbox Code Playgroud)

  • 正如您已经注意到的那样,设置`text`属性始终将插入符号放在文本的末尾.我做了一些实验,实际上已经找到了一种方法来做到这一点,而不会失去插入位置.请参阅下面的答案. (3认同)

Dan*_*ser 31

虽然所有其他答案确实有效(它们使输入为大写),但它们都存在不保留光标位置的问题(尝试在现有文本的中间插入一个字符).这显然是发生在的二传手UITextFields"的text属性,我还没有找到一种以编程方式恢复它(例如,恢复原来selectedTextRange不工作).

不过,好消息是,有一个直接的方式来更换的零件UITextField的(或UITextView的)文本,不从这个问题遭受:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    // not so easy to get an UITextRange from an NSRange...
    // thanks to Nicolas Bachschmidt (see http://stackoverflow.com/questions/9126709/create-uitextrange-from-nsrange)
    UITextPosition *beginning = textField.beginningOfDocument;
    UITextPosition *start = [textField positionFromPosition:beginning offset:range.location];
    UITextPosition *end = [textField positionFromPosition:start offset:range.length];
    UITextRange *textRange = [textField textRangeFromPosition:start toPosition:end];

    // replace the text in the range with the upper case version of the replacement string
    [textField replaceRange:textRange withText:[string uppercaseString]];

    // don't change the characters automatically
    return NO;
}
Run Code Online (Sandbox Code Playgroud)

有关这些方法的更多信息,请参阅文档UITextInput.