iOS中UITextFiled只能输入数字和小数点

#define NUMBERS @"0123456789."
 _contentTextFiled.delegate = self;

#pragma mark ---textfiled代理方法
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
        NSCharacterSet*cs;
        cs = [[NSCharacterSet characterSetWithCharactersInString:NUMBERS] invertedSet];
        NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
        BOOL basicTest = [string isEqualToString:filtered];
        if (!basicTest) {
            return NO;
        }else {
            return YES;
        }
}

用以上代理方法可实现只能输入数字和小数点

你可能感兴趣的:(ios,数字,Numbers,UItextFiled)