iPhone Force Textbox Input to Upper Case
Solution 1
Set autocapitalizationType
to UITextAutocapitalizationTypeAllCharacters
on the UITextField
.
See UITextInputTraits protocol (adopted by UITextField) for more details.
Solution 2
This solution is not fully satisfying.
Even with the autocapitalizationType
set to UITextAutocapitalizationTypeAllCharacters
, the user can still press caps to release the caps lock. And the textField.text = [textField.text stringByReplacingCharactersInRange:range withString:[string uppercaseString]]; return NO;
solution is not that great: we loose the editing point if the user edits the middle of the text (after a textField.text =
, the editing cursor goes to the end of the string).
I've done a mix of the two solution and here is what I propose: set UITextAutocapitalizationTypeAllCharacters
, and add the following code to the delegate of the 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;
}
Solution 3
While all the other answers do actually work (they make the input uppercase), they all have the problem that the cursor position is not retained (try inserting a character in the middle of the existing text). This apparently happens in the setter of UITextField
's text
property, and I have not found a way to restore it programmatically (for example, restoring the original selectedTextRange
does not work).
However, the good news is, that there is a direct way to replace parts of a UITextField
's (or UITextView
's) text, which does not suffer from this issue:
- (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;
}
For further information on these methods, see the documentation of UITextInput
.
Solution 4
This is my code, when i have 6 textFields
Hope you will get the point
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if (textField==self.textField6) {
self.textField6.text = [textField.text stringByReplacingCharactersInRange:range withString:[string uppercaseString]];
return NO;
}
return YES;
}
Solution 5
The simplest way would be to , implement the editing changed method of the text field and set the textfield's text value to upper case representation of the entered text
- (IBAction)TextFieldEditingChanged:(id)sender
{
_yourTextField.text = [_yourTextField.text uppercaseString];
}
hungbm06
Updated on June 09, 2020Comments
-
hungbm06 almost 4 years
How do I force characters input into a textbox on the iPhone to upper case?