swift textField 每四位加一个空格

  • 重写实现文本代理方法
//MARK: 文本代理方法
extension ChinabankWithdrawViewController:UITextFieldDelegate{
    
    //当文本变化时调用
    func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
        if self.textFieldCardNum == textField{
            //改变的字符
            self.changeCharacter = string
            //字符串改变的长度
            self.textFieldCardNumLength = (textField.text?.length)!
            //银行卡号为数不超过20位
            if self.textFieldCardNumLength == 24{
                self.textFieldShouldClear(textField)
            }
        }
        return true
    }
    
    //后退时调用
    func textFieldShouldClear(textField: UITextField) -> Bool {
        let index = self.textFieldCardNum.text?.endIndex.predecessor()
        self.textFieldCardNum.text = self.textFieldCardNum.text?.substringToIndex(index!)
        return true
    }
}
  • 为了防止无法后退的问题,声明一个观察属性变量,当文本长度增加时,继续执行每四位加一个空格的逻辑,否则清除后退
var changeCharacter = ""
    var textFieldCardNumLength: Int = 0 {
        willSet{
            //如果文本
            if textFieldCardNumLength < newValue{
                    // 四位加一个空格
                    if changeCharacter == " " {
                        if ((self.textFieldCardNum.text?.length)! - 2) % 5 == 0{
                            let index = self.textFieldCardNum.text?.endIndex.predecessor()
                            self.textFieldCardNum.text = self.textFieldCardNum.text?.substringToIndex(index!)
                        }
                    }else{
                        if (self.textFieldCardNum.text?.length)! % 5 == 0{
                                self.textFieldCardNum.text?.appendContentsOf(" ")
                        }
                    }
            }
        }
    }

以上代码实现不好,最终版:

// MARK: - 实现文本输入框代理方法
extension BankTransferViewController: UITextFieldDelegate {
    
    func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
        
        let maxCharOfCardNo = 25
        let maxCharOfMoney = 8
        
        if textField === self.textFieldCardNo {
            var text = NSString(string: textField.text!)
            
            let characterSet = NSCharacterSet(charactersInString: "0123456789")
            let nstring = NSString(string: string.stringByReplacingOccurrencesOfString(" ", withString: ""))
            if nstring.rangeOfCharacterFromSet(characterSet.invertedSet).location != NSNotFound {
                return false
            }
            
            text = text.stringByReplacingCharactersInRange(range, withString: string)
            text = text.stringByReplacingOccurrencesOfString(" ", withString: "")
            var newString = String(text).insertCharByInterval(" ", interval: 4)
            
            newString = newString.stringByTrimmingCharactersInSet(characterSet.invertedSet)
            
            if (newString.length >= maxCharOfCardNo) {
                return false;
            }
            
            textField.text = newString
            
            return false;
        }
        
        if(textField === self.textFieldMoney) {
            if (range.location>(maxCharOfMoney - 1)) {
                return false
            }
        }
        
        return true
        
    }
    
}

你可能感兴趣的:(swift textField 每四位加一个空格)