Upvote:113

Aprove answer
       self.yourTextField.autocapitalizationType = .allCharacters

Upvote:26

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {

    if textField == textFieldToUppercase {
        if string == "" {
            // User presses backspace
            textField.deleteBackward()
        } else {
            // User presses a key or pastes
            textField.insertText(string.uppercaseString)
        }
        // Do not let specified text range to be changed
        return false
    }

    return true
}

More Answer related to the Same Query

Upvote:20

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
    textField.text = (textField.text as NSString).stringByReplacingCharactersInRange(range, withString: string.uppercaseString)

    return false
}

Upvote:18

textField.autocapitalizationType = .allCharacters

More Answer related to the Same Query

Upvote:9

func textFieldDidChange(textField: UITextField) {
    textField.text = textField.text?.uppercaseString
}

Upvote:6

@objc func textFieldDidChange() {
    textField.text = textField.text?.uppercased()
}

Upvote:5

.none //Specifies that there is no automatic text capitalization.

.words //Specifies automatic capitalization of the first letter of each word.

.sentences //Specifies automatic capitalization of the first letter of each sentence.

.allCharacters //Specifies automatic capitalization of all characters, such as for entry of two-character state abbreviations for the United States.

More Answer related to the Same Query

Upvote:4

extension CurrentViewController: UITextFieldDelegate{

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {

        //refference to the textfield you want to target
        if textField.tag == 5{
            textField.text = (textField.text! as NSString).replacingCharacters(in: range, with: string.uppercased())
            return false
        }
        return true
    }
}

Upvote:1

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

    // Uppercase for string which you need 
    textField.text = [textField.text stringByReplacingCharactersInRange:range 
                                 withString:[string uppercaseString]];

    // return NO because You have already done it in above code
    return NO;
}

Upvote:1

@property (nonatomic, strong) IBOutlet UITextField *yourTextfield

// add target in code or use interface builder
[self.yourTextField addTarget:self 
                       action:@selector(uppercaseTextField)
             forControlEvents:UIControlEventEditingChanged];

- (IBAction)uppercaseTextField:(UITextField*)textField
{
    textField.text = [textField.text uppercaseString];
}

Upvote:1

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    if let text = textField.text, let textRange = Range(range, in: text) {
        let uppercasedString = string.uppercased()
        let updatedText = text.replacingCharacters(in: textRange, with: uppercasedString)
        if let selectedTextRange = textField.selectedTextRange {
            textField.replace(selectedTextRange, withText: uppercasedString)
            approveButtonState(vin: updatedText)
        }
        return string.isEmpty
    }
    return false
}

Upvote:0

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    var returnValue = true
    let lowercaseRange = string.rangeOfCharacter(from: CharacterSet.lowercaseLetters)
    if let _ = lowercaseRange?.isEmpty {
        returnValue = false
    }

    if !returnValue {
        textField.text = (textField.text! + string).uppercased()
    }

    return returnValue
}

Upvote:0

/**
 We take full control of the text entered so that lowercase cannot be inserted
 we replace lowercase to uppercase
*/
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {

    // No spaces allowed
    if string == " " {
        return false
    }

    // delete key pressed
    if string == "" {
        textField.deleteBackward()
        return false
    }

    // We only allow alphabet and numbers
    let numbersAndLettersSet = CharacterSet.alphanumerics
    if string.lowercased().rangeOfCharacter(from: numbersAndLettersSet) == nil {
        return false
    }

    // Add the entered text
    textField.insertText(string.uppercased())

    // Return false as we are doing full control
    return false
}

Upvote:0

class MyCustomTextField: UITextField {
...
addTarget(self, action: #selector(upperText), for: .editingChanged)
...
...
@objc private func upperText() {
    let textRange = selectedTextRange
    text = text?.uppercased()
    selectedTextRange = textRange
}

Upvote:0

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        if textField == txtEmail {
            textField.text = (textField.text! as NSString).replacingCharacters(in: range, with: string.uppercased())
            return false
        }
        return true
    }

Upvote:-1

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range
replacementString:(NSString *)string {
//--- Making uppercase ---//
        if (textField == yourTextField ) {
            NSRange lowercaseCharRange;
            lowercaseCharRange = [string rangeOfCharacterFromSet:[NSCharacterSet lowercaseLetterCharacterSet]];

            if (lowercaseCharRange.location != NSNotFound) {

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


}

Credit Goes to: stackoverflow.com

Related question with same questions but different answers