Created
December 24, 2017 06:54
-
-
Save anonymous/5efdd2e721b66bb82e116d1168e6fb76 to your computer and use it in GitHub Desktop.
currencyDelegate
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// CurrencyDelegate.swift | |
// TextFieldDelegate | |
// | |
// Created by Timothy Isenman on 12/10/17. | |
// Copyright © 2017 Timothy Isenman. All rights reserved. | |
// | |
import Foundation | |
import UIKit | |
class CurrencyDelegate: NSObject, UITextFieldDelegate { | |
var amount: Int = 0 | |
func currencyAmount() -> String? { | |
let formatter = NumberFormatter() | |
formatter.numberStyle = .currency | |
let amount = Double(self.amount/100) + Double(self.amount%100)/100 | |
return formatter.string(from: NSNumber(value: amount)) | |
} | |
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { | |
if let digit = Int(string) { | |
amount = amount * 10 + digit | |
textField.text = String(describing: currencyAmount()) | |
} else if string == "" { | |
amount = amount / 10 | |
textField.text = String(describing: currencyAmount()) | |
} | |
return false | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment