Contents

leodabus/currencyfield

Tiny SPM that provides a battle-tested currency input field for **UIKit** and a clean wrapper for **SwiftUI**.

Installation

Add the package via Xcode (File > Add Packages...) using your repo URL once you push it.

Usage

SwiftUI

import SwiftUI
import CurrencyField

struct ContentView: View {
    @State private var frAmount: Decimal = 0
    @State private var usAmount: Decimal = 0
    @FocusState private var isFocused: Bool

    var body: some View {
        VStack(alignment: .trailing, spacing: 16) {
            Text("French (fr_FR)")
            CurrencyFieldView(amount: $frAmount, locale: .init(identifier: "fr_FR"))
                .frame(width: 220)

            Text("US (en_US)")
            CurrencyFieldView(amount: $usAmount, locale: .init(identifier: "en_US"))
                .frame(width: 220)
                .focused($isFocused)
                .onAppear {
                    isFocused = true
                }
        }
        .padding()
        .multilineTextAlignment(.trailing)
    }
}

UIKit

import UIKit
import CurrencyField

final class ViewController: UIViewController {

    let currencyField = CurrencyField()

    override func viewDidLoad() {
        super.viewDidLoad()
        currencyField.locale = .init(identifier: "pt_BR")
        currencyField.maximum = 100_000
        currencyField.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(currencyField)

        NSLayoutConstraint.activate([
            currencyField.centerXAnchor.constraint(equalTo: view.centerXAnchor),
            currencyField.centerYAnchor.constraint(equalTo: view.centerYAnchor),
            currencyField.widthAnchor.constraint(equalToConstant: 220)
        ])

        currencyField.addTarget(self, action: #selector(changed), for: .editingChanged)
    }

    @objc private func changed() {
        print("decimal:", currencyField.decimal)
    }
}

Package Metadata

Repository: leodabus/currencyfield

Default branch: main

README: README.md