货币格式化程序SwiftUI

qlvxas9a  于 2023-05-21  发布在  Swift
关注(0)|答案(3)|浏览(122)

我试图格式化为用户输入一个值到文本字段。我已经有了下面的代码,但我找不到一种方法来做到这一点到文本字段。

import SwiftUI

struct ContentView: View {
    @State private var amount: String = ""
    @State private var formattedAmount: String = ""
 
    var body: some View {
        VStack {
            Text("Valor: \(formattedAmount)")
            
            TextField("Valor", text: $amount)
                .textFieldStyle(.roundedBorder)
                .padding()
                .keyboardType(.numberPad)
                .onChange(of: amount, perform: { value in
                    updateFormattedAmount()
                })
        }
    }

    private func updateFormattedAmount() {
        guard let number = Double(amount) else {
            formattedAmount = ""
            return
        }
        
        let formatter = currencyFormatter
        formattedAmount = formatter.string(from: NSNumber(value: number / 100)) ?? ""
    }

    private var currencyFormatter: NumberFormatter {
        let formatter = NumberFormatter()
        formatter.numberStyle = .currency
        formatter.currencyCode = "BRL"
        formatter.currencySymbol = "R$"
        formatter.minimumFractionDigits = 2
        formatter.maximumFractionDigits = 2
        formatter.groupingSeparator = "."
        formatter.usesGroupingSeparator = true
        return formatter
    }
}

这是我在UI上看到的:

我希望在文本域中显示我对formattedAmount var所做的操作。

bihw5rsg

bihw5rsg1#

我喜欢使用TextField的新format: API,例如。

struct ContentView: View {
    @State var amount: Decimal = 0 // always use Decimal for money
    @Environment(\.locale) private var locale // body will be called if they change their currency.

    var body: some View {

        TextField("Amount", value: $amount, format: .currency(code: locale.currency?.identifier ?? "USD")                
            
    }
}

struct Amount {
    var value: Decimal = 0
    let currencyCode = "USD"
}

struct ContentView: View {
    @State var amount = Amount()

    var body: some View {
        TextField("Amount", value: $amount.value, format: .currency(code: amount.currencyCode)
    }
}
olhwl3o2

olhwl3o22#

改变

@State private var amount: String = ""

TextField("Valor", text: $amount)

@State private var amount: Double = 0.0

TextField("Valor", value: $amount, formatter: currencyFormatter)

然后,当用户“提交”时,它将使用所有格式化程序设置更改该字段。

91zkwejq

91zkwejq3#

是的,上面的答案建议@lorem ipsum是正确的,但你必须明白的是,当编辑Textfield可以反映这些变化,一旦你关闭键盘。如果你想实现这一点,你必须在UITextFieldshouldChangeCharacterIn API中为UITextFieldDelegate编写Coordinators。另外,你已经写的代码,你不需要写那么多就可以达到相同的效果。你可以直接在TextField API上使用Formatter,如下面的代码。请看下面的代码。它会按照你想要的方式工作,但点击完成按钮。

struct ContentView: View {
    @State private var amount: Double = 0.0
    let currencyFormatter: NumberFormatter = {
        let formatter = NumberFormatter()
        formatter.currencyCode = "BRL"
        formatter.currencySymbol = "R$"
        formatter.minimumFractionDigits = 0
        formatter.maximumFractionDigits = 0
        formatter.usesGroupingSeparator = true
        return formatter
    }()
    
    var body: some View {
        VStack {
            Text("Valor: \(amount, specifier: "%.2f")")
            TextField("Valor", value: $amount, format: .currency(code: "BRL"))
                .textFieldStyle(.roundedBorder)
                .padding()
                .keyboardType(.numberPad)
        }
    }
}

同样,如果你不需要完成按钮,请回顾这篇文章,这是最接近的方法与你的问题相似。
How to Use Currency Input in a UITextField希望有帮助。

相关问题