user5370095
user5370095

Reputation:

Force lowercase - ios swift

I would like to force lowercase in an UITextfield when the user is typing. I came out so far with this code, but seems like it's not lowering characters.

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
    if string.characters.count == 0 {
        return true
    }

    let currentText = textField.text ?? ""
    let prospectiveText = (currentText as NSString).stringByReplacingCharactersInRange(range, withString: string.lowercaseString)

    switch textField {
    // Allow only lower-case vowels in this field,
    // and limit its contents to a maximum of 6 characters.
    case userNameTextField:
        return prospectiveText.characters.count <= 27
    default:
        return true
    }
}

Upvotes: 8

Views: 9818

Answers (4)

Sajjad Sarkoobi
Sajjad Sarkoobi

Reputation: 1064

if you want to convert all input characters to lower case you should do this code:

Swift 4:

in override func viewDidLoad() add this:

 textfield.addTarget(self, action: #selector(self.textFieldDidChange(_:)), for: UIControlEvents.editingChanged)

and then add this function to your class:

@objc func textFieldDidChange(_ textField: UITextField) {
        if let text:String = textfield.text {
            DispatchQueue.main.async {
                self.textfield.text = text.lowercased()
            }
        }

}

it is necessary to change it in main thread.

Upvotes: 0

Abhinav
Abhinav

Reputation: 38142

First you should set following property on your textfield to restrict auto capitalisation:

textfield.autocapitalizationType = UITextAutocapitalizationType.None

And this is how you can restrict it further:

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

    if let _ = string.rangeOfCharacterFromSet(NSCharacterSet.uppercaseLetterCharacterSet()) {
        // Do not allow upper case letters
        return false
    }

    return true
}

UPDATED FOR SWIFT 4

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

    if let _ = string.rangeOfCharacter(from: .uppercaseLetters) {
        // Do not allow upper case letters
        return false
    }

    return true
}

Upvotes: 18

Lobo
Lobo

Reputation: 156

for swift 3 users, the above code given by Abhinav is just converted to the following

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

    if let _ = string.rangeOfCharacter(from:NSCharacterSet.uppercaseLetters) {

        return false
    }

    return true
}

Upvotes: 1

tim_yng
tim_yng

Reputation: 2671

You could do like this and lowercase the entire string when something has changed.

textfield.addTarget(self, action: "textViewChanged", forControlEvents: .EditingChanged);


  func textViewChanged(){
        textfield.text = textfield.text?.lowercaseString;
    }

Upvotes: 3

Related Questions