Last active
June 12, 2019 09:01
-
-
Save BurakDizlek/90e9ee2fd00e1ab9f811bca79c532b3f to your computer and use it in GitHub Desktop.
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
// | |
// CurrencyEditField.swift | |
// TSoftShopper | |
// | |
// Created by Burak Dizlek on 12.06.2019. | |
// Copyright © 2019 Burak Dizlek. All rights reserved. | |
// | |
import Foundation | |
class UICurrencyTextField : UITextField, UITextFieldDelegate { | |
var amt:Int = 0 | |
override init(frame: CGRect) { | |
super.init(frame: frame) | |
self.delegate = self | |
} | |
required init?(coder aDecoder: NSCoder) { | |
super.init(coder: aDecoder) | |
} | |
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { | |
if let digit = Int(string){ | |
amt = amt * 10 + digit | |
self.text = updateAmount() | |
} | |
if string == "" { | |
amt = amt/10 | |
self.text = updateAmount() | |
} | |
return false | |
} | |
func updateAmount() -> String? { | |
let formatter = NumberFormatter() | |
formatter.numberStyle = .currency | |
let amount = Double(amt/100) + Double(amt%100)/100 | |
return formatter.string(from: NSNumber(value: amount)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment