With Currency Field you can create a numerical input field most commonly found in banking apps. The user types in the amount, and the digits fill in from the right. It uses a UITextField
for custom input, and a SwiftUI.Text
to display the formatted value which allows for easy styling. This also makes it possible to use the .focused()
modifier to update its first responder state.
Example:
dependencies: [
.package(url: "https://github.com/jtrinc/swiftui-currency-field.git", from: "1.0.0")
]
import SwiftUI
import CurrencyField
struct Content: View {
@State private var value: Int = 0
var body: some View {
Form {
HStack {
Spacer()
CurrencyField(value: $value)
.font(.largeTitle.monospaced())
}
}
}
}
Int
representing the amount in cents.import SwiftUI
import CurrencyField
struct Content: View {
@State private var value = 0
@State private var chosenLocale = Locale(identifier: "en_CA")
private var formatter: NumberFormatter {
let fmt = NumberFormatter()
fmt.numberStyle = .currency
fmt.minimumFractionDigits = 2
fmt.maximumFractionDigits = 2
fmt.locale = chosenLocale
return fmt
}
private let locales = [
Locale(identifier: "en_CA"),
Locale(identifier: "fr_FR"),
Locale(identifier: "ja_JP"),
]
var body: some View {
Form {
Picker(selection: $chosenLocale) {
ForEach(locales, id: \.self) {
if let cc = $0.currencyCode, let sym = $0.currencySymbol {
Text("\(cc) \(sym)")
}
}
} label: {
Text("Currency")
}
CurrencyField(value: $value, formatter: formatter)
}
}
}
This package was originally inspired by this tutorial: Currency TextField in SwiftUI
CurrencyField
is available under the MIT license. See the LICENSE for more info.