I am trying to format currency input in a textfield in Swift as the user inputs it.
So far, I can only format it successfully when the user finishes inputting:
For Swift 3.0
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
// Construct the text that will be in the field if this change is accepted
switch string {
case "0","1","2","3","4","5","6","7","8","9":
currentString += string
formatCurrency(currentString)
default:
if string.characters.count == 0 && currentString.characters.count != 0 {
currentString = String(currentString.characters.dropLast())
formatCurrency(currentString)
}
}
return false }
func formatCurrency(_ string: String) {
print("format \(string)")
let formatter = NumberFormatter()
formatter.numberStyle = .currency
formatter.locale = findLocaleByCurrencyCode("NGN")
let numberFromField = (NSString(string: currentString).doubleValue)/100
let temp = formatter.string(from: NSNumber(value: numberFromField))
self.amountTextField.text = String(describing: temp!.characters.dropFirst())
}
func findLocaleByCurrencyCode(_ currencyCode: String) -> Locale? {
let locales = Locale.availableIdentifiers
var locale: Locale?
for localeId in locales {
locale = Locale(identifier: localeId)
if let code = (locale! as NSLocale).object(forKey: NSLocale.Key.currencyCode) as? String {
if code == currencyCode {
return locale
}
}
}
return locale }