美文网首页iOS相关记录本
IOS-UITextField输入密码是密文时,实现点击删除按钮

IOS-UITextField输入密码是密文时,实现点击删除按钮

作者: 请叫我钱哥儿 | 来源:发表于2019-03-08 14:43 被阅读0次

开发语言:swift4.2
废话不多说,上代码
第一步,实现密文从最后向前一个个删除

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        
        let textFieldContent = textField.text as NSString?
        let textfieldContent2 = textFieldContent?.replacingCharacters(in: range, with: string)
      
        if  textField == secretInputField && string == "" {
            textField.text = textfieldContent2
            return false
        }
        return true
  }

这一步之后,会发现当光标从密文中间往前删除的话,删除一个密文光标就会跑的密文最后的位置,第二步,解决这个问题

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

        let textFieldContent = textField.text as NSString?
        let textfieldContent2 = textFieldContent?.replacingCharacters(in: range, with: string)
      
        if  textField == secretInputField  && string == "" {
            textField.text = textfieldContent2
            let start = textField.position(from:textField.beginningOfDocument, offset: range.location)
            let end = textField.position(from: start!, offset: 0)
            textField.selectedTextRange = textField.textRange(from: start!, to: end!)
            return false;
        }
        return true
  }

代码修改之后,会发现光标正常了,但是当密文密码中间一直删除到开头之后发现,光标之后的密文还是会一下都删除掉,这时候来看第三步
首先重写UITextField的deleteBackward()方法

class BaseTextField: UITextField {

        weak var baseTextFieldDelegate: BaseTextFieldDeleteDelegate?
    
        override func deleteBackward() {
            super.deleteBackward()
        
            guard let baseTextFieldDelegate = baseTextFieldDelegate else {
                print("baseTextFieldDelegate is nil.")
                return
            }
            baseTextFieldDelegate.baseTextFieldDeleteBackward(baseTextField: self)
        }
}

然后定义一个变量var secretString

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
       
        let textFieldContent = textField.text as NSString?
        let textfieldContent2 = textFieldContent?.replacingCharacters(in: range, with: string)
      
        if  textField == secretInputField && string == "" {
            textField.text = textfieldContent2
            let start = textField.position(from:textField.beginningOfDocument, offset: range.location)
            let end = textField.position(from: start!, offset: 0)
            textField.selectedTextRange = textField.textRange(from: start!, to: end!)
            secretString = secretInputField.text!
            return false;
        }
        return true
  }

实现BaseTextField的代理方法

func baseTextFieldDeleteBackward(baseTextField: BaseTextField) {
        
        if baseTextField == secretInputField {
            secretInputField.text = secretString
        }
 }

你以为到这里就结束了?再点点少年,你会发现我的天,当从中间删除密文时,删除完之后,光标又跑了最后面去了,来接着第五步

 func baseTextFieldDeleteBackward(baseTextField: BaseTextField) {
        
        if baseTextField == secretInputField {
            secretInputField.text = secretString
            if secretInputField.text != "" && secretInputField.isSecureTextEntry {

                /// 让光标始终停在在最初的位置
                let start = baseTextField.position(from:baseTextField.beginningOfDocument, offset: 0)
                let end = baseTextField.position(from: start!, offset: 0)
                baseTextField.selectedTextRange = baseTextField.textRange(from: start!, to: end!)
                return
            }
        }
    }

到了这里,你点啊点,终于发现密文没有问题了,你可以先笑一笑,因为明文又出现问题了,你发现明文删不动了,来最后一步

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
       
        let textFieldContent = textField.text as NSString?
        let textfieldContent2 = textFieldContent?.replacingCharacters(in: range, with: string)
      
        if  textField == secretInputField && string == "" {
            textField.text = textfieldContent2
            let start = textField.position(from:textField.beginningOfDocument, offset: range.location)
            let end = textField.position(from: start!, offset: 0)
            textField.selectedTextRange = textField.textRange(from: start!, to: end!)
            secretString = secretInputField.text!
            return false;
        }
        if  textField == secretInputField  && string != "" {
            textField.text = textfieldContent2;
            let start = textField.position(from:textField.beginningOfDocument, offset: range.location+1)
            let end = textField.position(from: start!, offset: 0)
            textField.selectedTextRange = textField.textRange(from: start!, to: end!)
            secretString = secretInputField.text!
            return false;
        }
        return true
  }

好了,这下天下太平了。

相关文章

网友评论

    本文标题:IOS-UITextField输入密码是密文时,实现点击删除按钮

    本文链接:https://www.haomeiwen.com/subject/biplpqtx.html