CocoaPods trunk is moving to be read-only. Read more on the blog, there are 19 months to go.
TestsTested | ✗ |
LangLanguage | Obj-CObjective C |
License | MIT |
ReleasedLast Release | Apr 2017 |
Maintained by Daniele Di Bernardo.
MPNumericTextField
is a class that extends the basic UITextField
to make you easily input formatted numbers in a text field.
It correctly handles decimal numbers, integer numbers, percentages and currency values, using either the current locale or a manually provided one.
Just add to your project these main files:
MPNumericTextField.h
MPNumericTextField.m
MPNumericTextFieldDelegate.h
MPNumericTextFieldDelegate.m
MPFormatterUtils.h
MPFormatterUtils.m
that can be found inside the Classes
folder.
You can create a very simple numeric text field using:
MPNumericTextField *textField = [[MPNumericTextField alloc] init];
This will create a text field that will handle the input of decimal numbers, using the current locale
and the local text field delegate implementation (see MPNumericTextFieldDelegate.{h|m}
).
You can change the default number style for the text field via the type
property. It can be set to one of these values:
MPNumericTextFieldDecimal
(default one)MPNumericTextFieldInteger
MPNumericTextFieldCurrency
MPNumericTextFieldPercentage
Eg. to change the number style of your text field to currency just do:
numericTextField.type = MPNumericTextFieldCurrency;
You can access the numeric value of your text field via the numericValue
property. Eg:
MPNumericTextField *textField = [[MPNumericTextField alloc] init];
textField.type = MPNumericTextFieldPercentage;
textField.numericValue = @(2.25);
// ... (make changes to the text field) ...
NSNumber *currentValue = textField.numericValue;
By default behavior the MPNumericTextField
will use the [NSLocale currentLocale]
locale. But you can easily change this property too:
numericTextField.locale = myCustomLocale;
When using MPNumericTextField
for displaying currencies, an old limitation was to be stuck with the currency code provided by NSLocale
.
Starting from v. 1.3.0 it's possible to set a custom currency code to represent those values using the desired currency:
numericTextField.currencyCode = @"JPY";
The MPNumericTextField
class seamlessly uses the MPNumericTextFieldDelegate
class as its own delegate. This delegate handles all the
logics that makes the text field draw numbers with the correct format while the user inserts numbers.
You can decide to set a new custom delegate for your objects, and the MPNumericTextField
will treat them as forward delegates
to make them work correctly with the existing one.
Unlike version 1.0.0 of this library, starting from 1.1.0 you can start using the standard -setDelegate:
and -delegate
methods
from UITextField
to set up your custom delegates without worrying about the basic numeric field functionalities.
You can choose to change the default placeholder color in your text field. Just use the placeholderColor
property:
numericTextField.placeholderColor = [UIColor redColor];
If this property is set to nil
, it will use the default system color.
Feel free to apport changes to the source code of this project and to send a pull request on Github to merge your contribution to the original project.
If you need further assistance, please contact me on Twitter: @marzapower.
Version 1.4.0
MPTextField
class opens the usage of placeholderColor
outside of the handling of the numeric text fieldVersion 1.3.0
MPFormatterUtils
Version 1.2.1
Version 1.2.0
MPNumericTextFieldInteger
Version 1.1.0
MPNumericTextFieldDelegate
MPNumericTextField
Version 1.0.0