JDFCurrencyTextField
JDFCurrencyTextField copied to clipboard
A drop-in replacement for UITextField to allow the user to enter monetary values.
JDFCurrencyTextField
JDFCurrencyTextField is a drop-in replacement for UITextField to make it easier to work with monetary values. JDFCurrencyTextField makes it easier to enter monetary values because it formats the cell's text as a numeric string while the user is editing it, and formats it back to a currency string when they have finished. It defaults to using the currentLocale for the formatting, but this can be changed if necessary.
Usage
JDFCurrencyTextField is a drop in replacement for UITextField.
To set the currency value of the text field, you can either set the field's decimalValue
, or you can alternatively you can set the text
property with a numeric string (not formatted as currency).
JDFCurrencyTextField *currencyField = [[JDFCurrencyTextField alloc] initWithFrame:frame];
currencyField.decimalValue = [[NSDecimalNumber alloc] initWithDouble:9.99];
or
JDFCurrencyTextField *currencyField = [[JDFCurrencyTextField alloc] initWithFrame:frame];
currencyField.text = @"100";
You can then retrieve the value again as an NSDecimalNumber
via the decimalValue
property.
You can change the locale that is used for the currency formatting. To do this, set your JDFCurrencyTextField's locale
property with your desired locale. You should do this before setting the text
property.
The keyboard defaults to UIKeyboardTypeDecimalPad. JDFCurrencyTextField also supports negative numbers, but you will need to set the keyboard type to UIKeyboardTypeNumbersAndPunctuation.
To run the example project, clone the repo, and run pod install
from the Example directory first.
Screenshots
The user enters a value:
<img src="Screenshots/JDFCurrencyTextField3.png") width="300"/>
When they finish editing, the value is formatted as a currency:
<img src="Screenshots/JDFCurrencyTextField4.png") width="300"/>
Installation
JDFCurrencyTextField is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "JDFCurrencyTextField"
Apps
JDFCurrencyTextField is being used in Live Poker Manager. It'd be cool to know if you're using it in any of your apps as well :)
Author
Joe Fryer, [email protected]
License
JDFCurrencyTextField is available under the MIT license. See the LICENSE file for more info.
Release Notes
2.1.4
Small improvement to the editing mode formatting: grouping separators are removed.
2.1.3
Invalid input is now defaulted to 0
, instead of an empty string.
2.1.2
Further localization improvements.
2.1.1
Localization improvements.
2.1
Initial CocoaPods release
2.0
numericValue
deprecated and replaced by decimalValue
.
1.0.1
Delegate message forwarding bug fixes.
1.0
Initial Release