A generic UI formatted textfield for iOS
How to use the component in your project:
-
Drag and drop the “UIFormattedTextField.h” and “UIFormattedTextField.m” files to your project.
-
Import the header in your class, where do you want to use it. #import “UIFormattedTextField.h”
-
If you are using xib or storyboard, change the class of the text field in the “Identity inspector” to UIFormattedTextField.
-
Create an instance variable to access the text field. IBOutlet UIFormattedTextField *currency;
-
Set the type and delegate of the text field. Don’t forget to import the delegate . currency.type = @”Currency”; currency.delegate = self; currency.numberOfDecimalPlaces = 3;
-
You have to change the delegate function , -(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { return YES; }
to
-(BOOL)textField:(UIFormattedTextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { return [textField shouldChangeCharactersInRange:range replacementString:string]; }
- You are done.