iOS UITextField 只能输入数字和小数点,只能有两位小数

.h
@property (nonatomic, strong)UITextField *text;

@property (nonatomic)BOOL isHaveDian;

@property (nonatomic, strong)NSTimer *timer;
@property (nonatomic, strong)UIAlertController *alert;
 
.m
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
   
    self.view.backgroundColor = [UIColor whiteColor];
   
    self.navigationController.navigationBar.translucent = NO;
   
    self.text = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 100, 30)];
    self.text.placeholder = @"123";
    self.text.delegate = self;
    [self.view addSubview:self.text];
   
    isHaveDian = YES;
   
}


//http://blog.csdn.net/chengyakun11/article/details/8494292
//textField.text 输入之前的值 string 输入的字符
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if ([self.text.text rangeOfString:@"."].location==NSNotFound) {
        isHaveDian=NO;
    }
    if ([string length]>0)
    {
        unichar single=[string characterAtIndex:0];//当前输入的字符
        if ((single >='0' && single<='9') || single=='.')//数据格式正确
        {
            //首字母不能为0和小数点
            if([self.text.text length]==0){
                if(single == '.'){
//                    [self alertView:@"亲,第一个数字不能为小数点"];
                    [self alertView:@"亲,第一个数字不能为小数点"];
                    [self.text.text stringByReplacingCharactersInRange:range withString:@""];
                    return NO;
                }
                if (single == '0') {
//                    [self alertView:@"亲,第一个数字不能为0"];
                    [self alertView:@"亲,第一个数字不能为0"];
                    [self.text.text stringByReplacingCharactersInRange:range withString:@""];
                    return NO;
                }
            }
            if (single=='.')
            {
                if(!isHaveDian)//text中还没有小数点
                {
                    isHaveDian=YES;
                    return YES;
                }else
                {
//                    [self alertView:@"亲,您已经输入过小数点了"];
                    [self alertView:@"亲,您已经输入过小数点了"];
                    [self.text.text stringByReplacingCharactersInRange:range withString:@""];
                    return NO;
                }
            }
            else
            {
                if (isHaveDian)//存在小数点
                {
                    //判断小数点的位数
                    NSRange ran=[self.text.text rangeOfString:@"."];
                    long tt=range.location-ran.location;
                    if (tt <= 2){
                        return YES;
                    }else{
//                        [self alertView:@"亲,您最多输入两位小数"];
                        [self alertView:@"亲,您最多输入两位小数"];
                        return NO;
                    }
                }
                else
                {
                    return YES;
                }
            }
        }else{//输入的数据格式不正确
//            [self alertView:@"亲,您输入的格式不正确"];
            [self.text.text stringByReplacingCharactersInRange:range withString:@""];
            return NO;
        }
    }
    else
    {
        return YES;
    }
}

- (NSString *)alertView:(NSString *)alertView
{
    self.alert = [UIAlertController alertControllerWithTitle:nil message:alertView preferredStyle:UIAlertControllerStyleAlert];
    [self presentViewController:self.alert animated:YES completion:nil];
   
   
    self.timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(tiAction) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop]addTimer:self.timer forMode:NSRunLoopCommonModes];
   
   
    return alertView;
}

- (void)tiAction
{
   
    __weak typeof(self) weakSelf = self;
    [self.alert dismissViewControllerAnimated:YES completion:^{
       
        [weakSelf.timer invalidate];
    }];
}
posted @ 2015-10-26 11:05  onlytyj  阅读(222)  评论(0)    收藏  举报