Wednesday, December 22, 2010

Decimal Textfield in iphone.

Decimal Textfield in iphone

There is no decimal point character in the iphone numberKeyPad. To take input for currency such as for rupees or dollar we need to
add the decimal point programmatically.

Simple steps for making decimal textfield for iphone.

1. We can make our own delegate which extends to the UITextFieldDelegate as:

DecimalTextField.h
   
#import <Foundation/Foundation.h>
#import "KeyboardHelper.h"

@interface DecimalTextFieldDelegate : NSObject<UITextFieldDelegate> {
   
}

@end


2. In the implementation of this interface define
- (BOOL) textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString*)textEntered.

we can manupulate and replace the text in the textfiled using this method. For example the following code will add a decimal point before the
second digit from the right of the input text.


DecimalTextFieldDelegate.m

#import "DecimalTextFieldDelegate.h"


@implementation DecimalTextFieldDelegate


//This method will add decimal before the second element from the right
-(NSString *) addDecimal : (NSString *)text{
   
    text = [text stringByReplacingOccurrencesOfString:@"." withString:@""];
    int i = [text intValue];
    NSString *test = [[NSNumber numberWithInt:i] stringValue];
    if ([test length] == 1) {
        test = [NSString stringWithFormat:@"%@%@", @"0", test];
    }
   
    NSString *lastTwo = [test substringFromIndex: [test length] -2];
    NSString *firstFew = [test length] >=3 ?[test substringToIndex:[test length] -2] : @"0";
   
    NSString *formatted = [NSString stringWithFormat:@"%@%@%@", firstFew, @".", lastTwo];
    return formatted;
}

- (BOOL) textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString*)textEntered
{
    //to handle enter key
    if ([textEntered isEqualToString:@"\n"]) {
        return YES;
    }
   
    //to handle backspace
    if ([textField.text length] && range.length == 1) {
        NSString *test = [[textField text] substringToIndex:[[textField text] length] -1];;
       
        textField.text = [self addDecimal:test];
        return NO;
    }
   
    if ([textField.text length] >=0 && [textEntered length] >0) {
        NSString *test = [NSString stringWithFormat:@"%@%@",[textField text],textEntered];
       
        textField.text = [self addDecimal:test];
       
        return NO;
    }
   
    return YES;
}
@end

3. Any uitextfield can be transformed to decimal textfield by setting its delegate to the DecimalTextFieldDelegate. This can be done as:
    UITextField *textField = [[UITextField alloc]init];
    DecimalTextFieldDelegate *delegate = [[DecimalTextFieldDelegate alloc]init];
    [textField setDelegate: atmDelegate];
   
  This will make the textField to show value in decimal.
    Input 123 in the textfield, it will be shown as 1.23.



By modifying the logic in
- (BOOL) textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString*)textEntered  we can format the string as we want.

No comments:

Post a Comment