开发者

how to show single dot using custom Number pad in iphone?

i am creating custom NumberPad

if (([[[UIDevice currentDevice] systemVersion] doubleValue] >= 4.1)) {
    inputBoxTextField.keyboardType = UIKeyboardTypeDecimalPad;
}

my problem is, i want to display dot only one time.

than开发者_运维知识库ks in advance.


If you want to allow the user to enter only one dot, you can use the delegate method

-(BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

And simply check if the textField already contains a dot.


- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {

NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];

NSNumber* candidateNumber;

NSString* candidateString = [textField.text stringByReplacingCharactersInRange:range withString:string];

range = NSMakeRange(0, [candidateString length]);

[numberFormatter getObjectValue:&candidateNumber forString:candidateString range:&range error:nil];

if (([candidateString length] > 0) && (candidateNumber == nil || range.length < [candidateString length])) {

    return NO;
}
else {
    return YES; } }


I use the following code. Works perfectly for me and with less code.

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {

    NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
    [numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
    NSString *decimalSymbol = [numberFormatter decimalSeparator];

    if(([textField.text rangeOfString:decimalSymbol].location != NSNotFound) &&
       ([string rangeOfString:decimalSymbol].location != NSNotFound)) {
        return NO;
    }
return YES; 
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜