Uppercase characters in UItextfield

前端 未结 17 1062
萌比男神i
萌比男神i 2020-12-24 04:34

I have a question about iOS UIKeyboard.

I have a UITextField and I would to have the keyboard with only uppercase characters.<

相关标签:
17条回答
  • 2020-12-24 05:32

    This is a different approach I used, where it does the following:

    1. Enforces capitalization as soon as the character is entered
    2. Catches situations where the user disables caps lock even if it textfield is set to auto caps
    3. Allows for easy editing
    4. Works with Swift 2.2

    First, register a notification to be updated whenever any changes occur in the textfield.

        textField.addTarget(self, action: #selector(YourClassName.textFieldDidChange(_:)), forControlEvents: UIControlEvents.EditingChanged)
    

    Then, implement textFieldDidChange.

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

    I chose this to avoid a situation where the user sees an uneven experience of some capitalized, but then changed once they move to the next character.

    0 讨论(0)
  • 2020-12-24 05:32
    /**
     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
    }
    
    0 讨论(0)
  • 2020-12-24 05:35

    Set your textfield type autocapitalizationType to UITextAutocapitalizationTypeAllCharacters on the UITextField

    self.yourTexField.autocapitalizationType = UITextAutocapitalizationTypeAllCharacters;
    

    After call delegate

    // delegate method

    - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
    {
        NSRange lowercaseCharRange = [string rangeOfCharacterFromSet:[NSCharacterSet lowercaseLetterCharacterSet]];
    
        if (lowercaseCharRange.location != NSNotFound) {
            textField.text = [textField.text stringByReplacingCharactersInRange:range
                                                                     withString:[string uppercaseString]];
            return NO;
        }
    
        return YES;
    }
    
    0 讨论(0)
  • 2020-12-24 05:35

    Set UITextField property autocapitalizationType to UITextAutocapitalizationTypeAllCharacters. This will make all characters to appear in upper case. Also visit here to find more about textfields

    0 讨论(0)
  • 2020-12-24 05:37

    You should avoid to use delegate method

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

    because this will trigger an unwanted behaviour with iOS 13 + QuickPath typing (the iOS' Swiftkey Keyboard counterpart).

    If you swipe on the keyboard and write "hello", it will write "HELLOHELLOHELLOHELLOHELLO" into the textfield. This is because the method is called multiple times and it appends the just changed text via textField.text = uppercasedValue.

    The right way is to observe the .editingChange event and uppercase then the value. For example:

    func awakeFromNib() {
        textField.addTarget(self, action: #selector(textFieldDidChange), for: .editingChanged)
    }
    

    and

    @objc func textFieldDidChange() {
        textField.text = textField.text?.uppercased()
    }
    
    0 讨论(0)
提交回复
热议问题