Uppercase characters in UItextfield

前端 未结 17 1063
萌比男神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:21

    Using the following text field delegate method it can be done:

    - (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;
                }
            }
    
    
    }
    

    Hope this helps.

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

    For those looking for a Swift version.

    Swift 4

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

    Original answer

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

    Using the Capitalization: All Characters property just forces keyboard to open with caps lock on, but lets the user to turned it off.

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

    Swift 4.0 Version:

    First set the delegate for the textfield you want to uppercase to the current ViewController (click drag from the textfield to the currentViewController to set the delegate).

    After add the extension:

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

    Here there's my situation and how I achieved to force the upper text:

    • custom class (UITextField subclass)
    • don't want to use delegate UITextFieldDelegate methods

    Solution proposed from @CodeBender was pretty much what I was looking for but the cursor always jump to the end as noticed from @Dan.

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

    This will set the cursor always in the correct position (where it was) even if user adds text in "the middle".

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

    The syntax is now

    Swift 2

    textField.autocapitalizationType = UITextAutocapitalizationType.AllCharacters
    

    Swift 3

    textField.autocapitalizationType = .allCharacters
    
    0 讨论(0)
  • 2020-12-24 05:31

    SwiftUI

    For SwiftUI the Syntax for autocapitalization and Keyboard type selection is:

    TextField("Your Placeholder", text: $emailAddress)
         .keyboardType(.emailAddress)
         .autocapitalization(.none)
    

    You can use the following options for autocapitalization:

    .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.
    
    0 讨论(0)
提交回复
热议问题