1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
//
// 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