Skip to content

Instantly share code, notes, and snippets.

@gleue
Last active January 5, 2022 03:41
Show Gist options
  • Save gleue/9806181 to your computer and use it in GitHub Desktop.
Save gleue/9806181 to your computer and use it in GitHub Desktop.
Force input into UITextField to uppercase
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
// Inspiration: http://stackoverflow.com/a/6265614 and http://stackoverflow.com/a/13388037
//
// Check if the added string contains lowercase characters.
// If so, those characters are replaced by uppercase characters.
//
NSRange lowercaseCharRange = [string rangeOfCharacterFromSet:[NSCharacterSet lowercaseLetterCharacterSet]];
if (lowercaseCharRange.location != NSNotFound) {
if (textField.text.length == 0) {
// Add first character -- and update Return button
//
// 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,
// which does not matter since the caret is at the end
// of the (empty) text fiueld anyways.
//
textField.text = [textField.text stringByReplacingCharactersInRange:range withString:[string uppercaseString]];
} else {
// Replace range instead of whole string in order
// not to lose input caret position -- but this will
// not update the Return button, which is not necessary
// here anyways
//
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];
[textField replaceRange:textRange withText:[string uppercaseString]];
}
return NO;
}
return YES
}
@MohamedAymenHADDAD
Copy link

SWIFT

@objc func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        guard
            let givenText = textField.text as NSString?,
            givenText.rangeOfCharacter(from: .lowercaseLetters).location != NSNotFound
        else { return true }
        
        let beginning = textField.beginningOfDocument
        
        if
            let start = textField.position(from: beginning, offset: range.location),
            let end = textField.position(from: start, offset: range.length),
            let textRange = textField.textRange(from: start, to: end)
        {
            textField.replace(textRange, withText: string.uppercased())
        }
        else
        {
            textField.text = givenText.replacingCharacters(in: range, with: string.uppercased())
        }
        
        return false
    }

@satyam90
Copy link

satyam90 commented Jan 5, 2022

This is almost good, but the first character will always be small

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment