//
//  SalesInputTotalVIew.m
//  vanke
//
//  Created by Z on 16/4/20.
//  Copyright © 2016年 gomore. All rights reserved.
//

#import "SalesInputTotalVIew.h"

@interface SalesInputTotalVIew ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UIView *viewRedBac;

@end
@implementation SalesInputTotalVIew


- (void)awakeFromNib {
    [super awakeFromNib];
    self.viewRedBac.layer.cornerRadius = 7;
    self.textFieldBillCount.delegate = self;
}

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

{
    NSArray *valideInput = @[@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"-",@""];
    if ([valideInput containsObject:string]) {
        if (textField.text.length > 0 && [string isEqualToString:@"-"]) {
            return NO;
        }
        NSMutableString * futureString = [NSMutableString stringWithString:textField.text];
        [futureString  insertString:string atIndex:range.location];
        
        if ([futureString hasPrefix:@"0."] ||
            [futureString hasPrefix:@"00"] ||
            [futureString hasPrefix:@"-0"] ||
            futureString.length > 6) {
            return NO;
        }
        
        NSInteger flag = 0;
        NSInteger pointNum = 0;//用于标记-的数量，只能有一个-
        const NSInteger limited = 20;//限制小数点后面的位数
        
        for (int i = (int)futureString.length - 1; i>=0; i--) {
            if ([futureString characterAtIndex:i] == '-') {
                pointNum++;
                if (pointNum == 2) {
                    
                    return NO;
                    
                }
            }
        }
        
        for (int i = (int)futureString.length - 1; i>=0; i--) {
            
            if ([futureString characterAtIndex:i] == '-') {
                
                if (flag > limited) {
                    
                    return NO;
                    
                }
                
                break;
                
            }
            
            flag++;
            
        }
        
        
        return YES;
        
    }else{
        return NO;
    }
    
    
    
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
