Force lowercase - ios swift

元气小坏坏 提交于 2019-12-10 14:49:44

问题


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

回答1:


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
}



回答2:


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



回答3:


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
}



回答4:


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.



来源:https://stackoverflow.com/questions/33195946/force-lowercase-ios-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!