5

I am trying to achieve the following functionality for SMS Verification code. How would I achieve it? After looking into a bunch of libraries in Github, none was similar enough to this design, which is widely used in the industry.

I tried implementing it with six textfields but found a number of problems as a lot of work, blocking all but the first textfield for initial input, laggy move of firstResponder and whatnot, what made me wonder if having 6 textfields is really the best approach.

Rounding the borders is piece of cake. The hard part is the functionality (i.e the cursor moving smoothly, getting back and forth, making all of them red when input is wrong, etc)

What do you guys think? How could I achieve such behavior/functionality?

enter image description here

rgoncalv
  • 5,825
  • 6
  • 34
  • 61

4 Answers4

7

Try Below Code. (Currently it contains 4 UITextField, change it as per your requirement.)

extension ConfirmationCodeViewController: UITextFieldDelegate {

    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        if !(string == "") {
            textField.text = string
            if textField == txtFirst {
                txtSecond.becomeFirstResponder()
            }
            else if textField == txtSecond {
                txtThird.becomeFirstResponder()
            }
            else if textField == txtThird {
                txtForth.becomeFirstResponder()
            }
            else {
                textField.resignFirstResponder()
            }
            return false
        }
        return true
    }

    func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
        if (textField.text?.count ?? 0) > 0 {

        }
        return true
    }
}
4

Try this (make sure textField tags are added and the delegate is connected)

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    if (string != "") {
        
        if (textField.text == "") {
            textField.text = string
            let nextResponder: UIResponder? = view.viewWithTag(textField.tag + 1)
            if (nextResponder != nil) {
                nextResponder?.becomeFirstResponder()
            }
        }
        return false
    } else {
        
        textField.text = string

        let nextResponder: UIResponder? = view.viewWithTag(textField.tag - 1)
        if (nextResponder != nil) {
            nextResponder?.becomeFirstResponder()
        }
        return false
    }
}
2

Please have a look to this git hub library...

PinCodeTextField

Your can also customise the number of input textfields.

Sarabjit Singh
  • 1,814
  • 1
  • 17
  • 28
  • 1
    thanks, this is what i've searching for so long, what query should i search on google for cocoa library like this? – alexringo Feb 09 '18 at 09:02
1
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        // On inputing value to textfield
        if ((textField.text?.characters.count)! < 1  && string.characters.count > 0 ){

            let nextTag = textField.tag + 1

            // get next responder
            var nextResponder = textField.superview?.viewWithTag(nextTag)

            if (nextResponder == nil){
                nextResponder = textField.superview?.viewWithTag(1)
            }
            textField.text = string;
            //resign at 4th textfield
            if textField.tag == 4{
                txt4.resignFirstResponder()
            }else{
                nextResponder?.becomeFirstResponder()
            }
            return false
        }
}

Try above code, I have taken 4 UITextFields and resign it to last textfield and you can take as much you want it.

devang bhatt
  • 460
  • 2
  • 15