Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

implemeting a text field input like an atm machines input

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 use 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.

like image 770
MBU Avatar asked Feb 18 '11 15:02

MBU


2 Answers

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.

like image 82
SomeRandomGuy Avatar answered Oct 20 '22 00:10

SomeRandomGuy


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 a static NSString.
  • In the viewdidload fn, set enteredtext = @"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).
like image 45
Nazik Avatar answered Oct 20 '22 00:10

Nazik