Creating a Subclass of UITextField in Swift to Support Currency Input

Creating a Subclass of UITextField in Swift to Support Currency Input

Creating a UITextField to handle currency input might seem easy enough until you actually start writing the code. The first issue you will probably come across is how are you going to restrict the user from entering anything other than numbers.

Creating a UITextField to handle currency input might seem easy enough until you actually start writing the code. The first issue you will probably come across is how are you going to restrict the user from entering anything other than numbers. Then you might realize that forcing users to enter periods and commas isn’t the best user experience. Here are some additional issues you will run into:

  • Not all regions and countries have their currency symbol on the left like in the United States.
  • Some regions and countries switch the placement of commas and periods.
  • Some currencies are formatted as whole numbers instead of two decimal places (ie. Japanese Yen JPY).
  • If you want to save the entered currency to your database as a number (Double, Float, etc), you will “clean” the currency string so your app doesn’t crash when you try to use the amount in a calculation.

At the time of writing, Swift has 903 locale and currency code combinations to select from. You can retrieve all these by looping through Locale.availableIdentifiers.

Like I alluded to at the beginning, there are more things you need to consider than originally thought.

ios currency object-oriented programming swift

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

How to Find Ulimit For user on Linux

Explains how to find ulimit values of currently running process or given user account under Linux using the 'ulimit -a' builtin command.

MEAN Stack Tutorial MongoDB ExpressJS AngularJS NodeJS

MEAN Stack Tutorial MongoDB ExpressJS AngularJS NodeJS - We are going to build a full stack Todo App using the MEAN (MongoDB, ExpressJS, AngularJS and NodeJS). This is the last part of three-post series tutorial.

Creating RESTful APIs with NodeJS and MongoDB Tutorial

Creating RESTful APIs with NodeJS and MongoDB Tutorial - Welcome to this tutorial about RESTful API using Node.js (Express.js) and MongoDB (mongoose)! We are going to learn how to install and use each component individually and then proceed to create a RESTful API.

systemctl List All Failed Units/Services on Linux

Explains how to use the systemctl command to list all failed units or services on Debian, Ubuntu, CentOS, Arch, Fedora, and other Linux distros.

CentOS Linux 8.2 Released and Here is How to Upgrade it

CentOS Linux 8.2 (2004) released. This release adds corrections for security issues based upon RHEL 8.2 souce and here is how to upgrade it.