I have a text field in an iphone app and i would like to implement the input similar to an atms input where all you have to do is enter digits (no decimal character input required). I want to be able to just u开发者_如何学Pythonse the numberpad for this text field. For example, the text field initially displays:
0.00
If the user enters the sequence 1234 the text field will then look like this:
12.34
It will also have to update if the user pushes the delete button.
For formatting, I think you'll be very happy with the details in this other SO post describing how to use NSNumberFormatter to display leading and trailing zeros. Pulled and tweaked from that post:
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
[formatter setFormat:@"##0.00"];
NSNumber* input; // Hook in your input
NSString* output = [NSString stringWithFormat:@"%@", [formatter stringFromNumber:input]]; // Hook to your text field
In order to convert an input of 1234 into 12.34 (and presumably an input of 12 into 0.12), consider that you are always shifting the decimal of the input two places.
// int storedInput has already been set to 1234
int input = 5; // Next button that was pressed
storedInput = storedInput * 10 + input; // 1234 * 10 + 5 = 12345
float displayedValue = input * .01; // 123.45
NSNumber* output = [NSNumber numberWithFloat:displayedValue];
These two code snippets should give you an idea of how to implement these concepts in your program. As for deletion...simply set storedInput to 0. It will be up to you to call these pieces of code every time there is a new input from the keyboard in order to refresh the value.
I succeed with the following code
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if(range.length==1)
{
if(enteredtext.length>1)
enteredtext = [enteredtext substringWithRange:NSMakeRange(0,enteredtext.length-1)];
}
else
{
if(enteredtext.length<9)
enteredtext = [NSString stringWithFormat:@"%@%@",enteredtext,string];
}
double amountindecimal = [enteredtext floatValue];
double res=amountindecimal * pow(10, -2);
NSString * neededstring = [NSString stringWithFormat:@"%.2f",res];
if([neededstring isEqualToString:@"0.00"])
enteredtext = @"0";
textField.text = neededstring;
return NO;
}
Notes:
enteredtext
is astatic NSString
.- In the
viewdidload
fn, setenteredtext = @"0";
- Set the UITextfield Initial text to 0.00
- Set the Keyboardtype of the Textfield to NumberPad.
- This code will only work upto 8 digits(999999.99).
Simple, just hook up the editingChanged event of the text field object to a method in your class, and every time the "editingChanged" method is called, remove all your "extra" characters (in your case the decimal point) like this:
NSString *newContents = self.AtmTextField.text;
newContents = [newContents stringByReplacingOccurrencesOfString:@"." withString:@""];
then reformat the newContents as you like, and re-set it as the text of the UITextField
I have just opened sourced SATextField which has this functionality built-in. Merely set its fixedDecimalPoint
to YES
.
EDIT: I have created a new project that contains this functionality: STAControls (specifically, use STAATMTextField
-- it even supports .
entry!). The difference is that this project has been implemented as a legitimate subclass of UITextField
whereas SATextField
was merely a container class housing a UITextField
instance.
My advice? Get rid of the textfield. Get rid of the standard keyboard and add your own UIButtons for the task.
I did something similar for an app. I started with a UITextField and the default keyboard, but after a while I figured out that it will be much much easier if I use 12 UIButtons and one UILabel.
精彩评论