Input currency format on a UITextfield

UITextField for currency input. Written in Swift.

Source: Author

Textfield can be create use currency text fields with UITextField subclass, how to make it first add a target for UIControlEvents is “.editingChanged” next step Add a selector method to filter numbers from your text field string, after filtering out all non-digits from your string, you can reformat your number again using NumberFormatter.

Format currency can you change in number format “.currentSyimbol” this example use dollar currency ($), so by using textfield “.editingChanges” in selector action it will be occur while typing, any changes when typing currency will remain.

After you finish typing it will appear in the textfield in the format that was set earlier. this extension string make it easy to you. If you use carthage you can set this github into your project https://github.com/tebs1200/swift-currency-field.

I also added an add handle for data binding, it will look like this if you add the add button earlier. Which is can also be added to the list table view. If you want to send (Post Api) this data, I think you should convert to Int with parameters.

Source: Author

For count string characters in swift2 use “self.characters.count” or swift4+ keep like code above is “self.count”, now run your the project.

For SoureCode I get from: https://stackoverflow.com/a/40469426/8366535

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store