I would like to change the input format of my textfield.
I want to give my textfield a currency format, like this:
$5 $55 $556 $5.560 $55.560 $556.001
This is my code:
func currencyToString() -> String? {
let formatter = NumberFormatter()
formatter.locale = Locale(identifier: "es_AR")
formatter.numberStyle = .currency
formatter.usesGroupingSeparator = true
return formatter.string(from: NSNumber(value: Int(self) ?? 0)
}
This is my ViewController with RxSwift, But i think this is wrong, because when i run the app i see $0.00 and then when i type 5 for example I get $50.00.
_view.amountTextField.rx.controlEvent(.editingDidBegin)
.subscribe(onNext: { [weak self] in
let cur = self?._view.amountTextField.text?.currencyToString()
self?._view.amountTextField.text = cur
}).disposed(by: disposeBag)
AmountTextField:
_view.amountTextField.rx.text.orEmpty
.withUnretained(self)
.subscribe(onNext: { owner, amount in
owner.viewModel.configureLabelAlert(amount: amount, label: owner._view.alertLabel, button: owner._view.dateButton)
}).disposed(by: disposeBag)
View Model:
func configureLabelAlert(amount: String, label: UILabel, button: UIButton) {
minAmount.observe(on: MainScheduler.instance)
.subscribe(onNext: { minAmount in
let minAmounInt = minAmount / 100
let amountInt = Int(amount) ?? 0
if amountInt > 0 && amountInt < minAmounInt {
label.isHidden = false
button.isEnabled = false
button.alpha = 0
} else {
label.isHidden = true
button.isEnabled = true
button.alpha = 1
}
})
.disposed(by: disposeBag)
}