TextFiled 控制键盘只可以输入数字(TextFiled only allow number input)

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    
    BOOL shouldInput = YES;
    
    NSNumberFormatter * nf = [[NSNumberFormatter alloc] init];
    [nf setNumberStyle:NSNumberFormatterDecimalStyle];
    
    NSString * newString = [NSString stringWithFormat:@"%@%@",textField.text,string];
    NSNumber * number = [nf numberFromString:newString];
    
    if (number)
        shouldInput = YES;
    else
        shouldInput = NO;

    if ([string isEqualToString:@"-"] || !string.length) {
        shouldInput = YES;
    }
    
     return shouldInput;
}

posted @ 2015-12-16 15:05  shujucn  阅读(314)  评论(0编辑  收藏  举报