How to achieve that placeholder text disappears character by character in UITextField

后端 未结 6 1854
挽巷
挽巷 2021-02-18 20:28

Can you please help me.

In UITextField when we provide a placeholder text its placeholder string will be gone when we enter any character. How can I achieve

6条回答
  •  遥遥无期
    2021-02-18 21:03

    Instead of using placeholder text, use a UILabel below your textfield and give the same font style to both. the label text should be like "- - - - -"

    And when user starts typing on textfield give a space after each character press.

    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    
        if textField.text?.characters.count == 0 && string.characters.count != 0 {
            textField.text = textField.text! + " "
        }
         else {
            return false
        }
    
        if textField.text?.characters.count == 1 && string.characters.count != 0 {
            textField.text = textField.text! + " "
        }
        else {
            return false
        }
    
        if textField.text?.characters.count == 2 && string.characters.count != 0 {
            textField.text = textField.text! + " "
        }
        else {
            return false
        }
        if textField.text?.characters.count == 3 && string.characters.count != 0 {
            textField.text = textField.text! + " "
        }
        else {
            return false
        }
    
        return true
    }
    

提交回复
热议问题