UITextField 显示格式为没有小数的货币

Ben*_*Ong 1 uitextfield ios swift

我试图让UITextField输入以货币格式永久显示,即使用户正在编辑。它应该像这样工作:

$0
Run Code Online (Sandbox Code Playgroud)

用户类型 1

$1
Run Code Online (Sandbox Code Playgroud)

用户类型 2

$12
Run Code Online (Sandbox Code Playgroud)

用户类型 0

$120
Run Code Online (Sandbox Code Playgroud)

用户类型 0

$1,200
Run Code Online (Sandbox Code Playgroud)

用户类型 0

$12,000
Run Code Online (Sandbox Code Playgroud)

我设法使用NumberFormatter类似的方法获得最终格式

textField.text = (numberFormatter.string(from: NSNumber(value: Int(textField.text!)!)))?.components(separatedBy: ".")[0]
Run Code Online (Sandbox Code Playgroud)

但是一旦我在 textField 中格式化文本,后续输入将导致错误,如:

$1,000
Run Code Online (Sandbox Code Playgroud)

用户类型 0

$1,0000 //number formatter cannot recognise this as a format of currency
Run Code Online (Sandbox Code Playgroud)

无论如何我可以操纵输入以允许用户只需要输入数字,但该值以正确的货币格式显示,带有符号加逗号 - 例如 $1,200,000。不管数字的数量 - 但逻辑上最多 10 位就足够了。

我目前的 textFieldshouldChangeCharactersInRange是这样的:

    textField.text = numberFormatter.number(from: textField.text!)?.description

    if(range.length == 1){  //Backspace
        if(textField.text?.characters.count == 1){
            textField.text = "$0"
            return false
        }
        return true
    }
    if(string.trimmingCharacters(in: CharacterSet.decimalDigits.inverted) == ""){
        textField.text = (numberFormatter.string(from: NSNumber(value: Int(textField.text!)!)))?.components(separatedBy: ".")[0]
        return false
    }
    if(string == ".") {
        textField.text = (numberFormatter.string(from: NSNumber(value: Int(textField.text!)!)))?.components(separatedBy: ".")[0]
        return false
    }

    textField.text = (numberFormatter.string(from: NSNumber(value: Int(textField.text!.append(string)!)!)))?.components(separatedBy: ".")[0]
    return false
Run Code Online (Sandbox Code Playgroud)

Mat*_*att 6

试试下面的代码

extension ViewController: UITextFieldDelegate {

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

        let text: NSString = (textField.text ?? "") as NSString
        let finalString = text.replacingCharacters(in: range, with: string)

        // 'currency' is a String extension that doews all the number styling
        amountTextField.text = finalString.currency

        // returning 'false' so that textfield will not be updated here, instead from styling extension
        return false
    }
}

extension String {
    var currency: String {
        // removing all characters from string before formatting
        let stringWithoutSymbol = self.replacingOccurrences(of: "$", with: "")
        let stringWithoutComma = stringWithoutSymbol.replacingOccurrences(of: ",", with: "")

        let styler = NumberFormatter()
        styler.minimumFractionDigits = 0
        styler.maximumFractionDigits = 0
        styler.currencySymbol = "$"
        styler.numberStyle = .currency

        if let result = NumberFormatter().number(from: stringWithoutComma) {
            return styler.string(from: result)!
        }

        return self
    }
}
Run Code Online (Sandbox Code Playgroud)