光标移至格式化格式化的十进制文本字段结束时结束-Swift

时间:2018-11-23 03:57:07

标签: swift uitextfield nsformatter

我正在格式化UITextField,以使其在键入时变为逗号分隔(十进制样式)。

所以12345678变成12,345,678

现在,当我编辑UITextField时,说我想删除5,那时我在5之后点击并删除它,但是光标立即移至文本结尾,即8之后。

这是我的代码,用于在键入时格式化小数:

func checkTextField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    if ((string == "0" || string == "") && (textField.text! as NSString).range(of: ".").location < range.location) {
        return true
    }
    let allowedCharacterSet = NSCharacterSet(charactersIn: "0123456789.").inverted
    let filtered = string.components(separatedBy: allowedCharacterSet)
    let component = filtered.joined(separator: "")
    let isNumeric = string == component
    if isNumeric {
        let formatter = NumberFormatter()
        formatter.numberStyle = .decimal
        let newString = (textField.text! as NSString).replacingCharacters(in: range, with: string)
        let numberWithOutCommas = newString.replacingOccurrences(of: ",", with: "")
        let number = formatter.number(from: numberWithOutCommas)
        if number != nil {
            var formattedString = formatter.string(from: number!)
            if string == "." && range.location == textField.text?.count {
                formattedString = formattedString?.appending(".")
            }
            textField.text = formattedString
        } else {
            textField.text = nil
        }
    }
    return false
}

它被这样称呼:

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    let textFieldText = (textField.text! as NSString).replacingCharacters(in: range, with: string)
    let checkForCurrency = checkTextField(textField, shouldChangeCharactersIn: range, replacementString: string)
    return checkForCurrency
}

我尝试了以下尝试,但没有成功:

Solution 1

Solution 2

光标移动的原因是什么? 它应该类似于this链接中的内容 任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:1)

  

使用以下命令更新您的自定义功能:

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

        let textFieldText = (textField.text! as NSString).replacingCharacters(in: range, with: string)
        if ((string == "0" || string == "") && (textField.text! as NSString).range(of: ".").location < range.location) {
            return true
        }

        var currentPosition = 0
        if let selectedRange = textField.selectedTextRange {
            currentPosition = textField.offset(from: textField.beginningOfDocument, to: selectedRange.start)
        }

        let allowedCharacterSet = NSCharacterSet(charactersIn: "0123456789.").inverted
        let filtered = string.components(separatedBy: allowedCharacterSet)
        let component = filtered.joined(separator: "")
        let isNumeric = string == component
        if isNumeric {
            let formatter = NumberFormatter()
            formatter.numberStyle = .decimal
            let newString = (textField.text! as NSString).replacingCharacters(in: range, with: string)
            let numberWithOutCommas = newString.replacingOccurrences(of: ",", with: "")
            let number = formatter.number(from: numberWithOutCommas)

            if number != nil {
                var formattedString = formatter.string(from: number!)
                if string == "." && range.location == textField.text?.count {
                    formattedString = formattedString?.appending(".")
                }
                textField.text = formattedString

                if(textFieldText.count < formattedString?.count ?? 0){
                    currentPosition = currentPosition + 1
                }
            }else{
                textField.text = nil
            }
        }

        if(string == ""){
            currentPosition = currentPosition - 1
        }else{
           currentPosition = currentPosition + 1
        }

        if let newPosition = textField.position(from: textField.beginningOfDocument, offset: currentPosition) {
            textField.selectedTextRange = textField.textRange(from: newPosition, to: newPosition)
        }
        return false
    }