implemeting a text field input like an atm machines input
Asked Answered
H

5

5

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.

Hawkeyed answered 18/2, 2011 at 15:7 Comment(0)
M
6

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.

Mccrea answered 18/2, 2011 at 15:36 Comment(1)
NSNumberFormatter setFormat is not available in iOS.Foremast
M
3

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).
Maemaeander answered 9/10, 2013 at 11:20 Comment(0)
C
1

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

Convector answered 18/2, 2011 at 15:31 Comment(0)
B
1

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.

Botulin answered 8/5, 2013 at 12:41 Comment(0)
F
0

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.

Forging answered 18/2, 2011 at 15:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.