JLChen
2021-10-08 f8457b624a75bf8e41489b74f66009eee6891b8b
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
//
//  Copyright © 2016年 dahua. All rights reserved.
//
 
#define EMOJI                      @"[^\\u0020-\\u007E\\u00A0-\\u00BE\\u2E80-\\uA4CF\\uF900-\\uFAFF\\uFE30-\\uFE4F\\uFF00-\\uFFEF\\u0080-\\u009F\\u2000-\\u201f\r\n]"
#define SPACE                      @"[//s//p{Zs}]"
#define LC_ILLEGALCHAR             @"[•€`~!#$%^&*+=|{}()':;'@,\\[\\]<>/?~!#¥%⋯⋯&*()——+|{}【】‘;:\"”“’。,、?]"
#define LCTextFieldDefaultBlankMsg @"Sorry, contents cannot be blank"
 
#import "LCTextField.h"
#import "UITextField+LeChange.h"
#import "UIFont+Dahua.h"
 
//抽象私有代理类, 封装UITextFieldDelegate代理方法, 默认返回默认值
@interface LCTextFieldSupport : NSObject <UITextFieldDelegate>
//设置代理类属性, 接受外部传入的代理
@property (nonatomic, weak) id<UITextFieldDelegate> delegate;
@property (nonatomic, assign) BOOL checkOnTextChanged;
@property (nonatomic, assign) BOOL checkOnResign;
 
@end
 
@implementation LCTextFieldSupport
@synthesize delegate, checkOnTextChanged, checkOnResign;
 
#pragma mark - UITextFieldDelegate
 
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    if ([delegate respondsToSelector:@selector(textFieldShouldBeginEditing:)]) {
        return [delegate textFieldShouldBeginEditing:textField];
    }
    return YES;
}
 
- (void)textFieldDidBeginEditing:(UITextField *)textField {
    if ([delegate respondsToSelector:@selector(textFieldDidBeginEditing:)]) {
        return [delegate textFieldDidBeginEditing:textField];
    }
}
 
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
    if ([delegate respondsToSelector:@selector(textFieldShouldEndEditing:)]) {
        return [delegate textFieldShouldEndEditing:textField];
    }
    return YES;
}
 
- (void)textFieldDidEndEditing:(UITextField *)textField {
    if ([delegate respondsToSelector:@selector(textFieldDidEndEditing:)]) {
        return [delegate textFieldDidEndEditing:textField];
    }
    if (checkOnResign) {
        [(LCTextField *)textField lc_checkIt];
    }
}
 
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if ([delegate respondsToSelector:@selector(textField:shouldChangeCharactersInRange:replacementString:)]) {
        return [delegate textField:textField shouldChangeCharactersInRange:range replacementString:string];
    }
    if (checkOnTextChanged) {
        [(LCTextField *)textField lc_checkIt];
    }
    return YES;
}
 
- (BOOL)textFieldShouldClear:(UITextField *)textField {
    if ([delegate respondsToSelector:@selector(textFieldShouldClear:)]) {
        return [delegate textFieldShouldClear:textField];
    }
    return YES;
}
 
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    if ([delegate respondsToSelector:@selector(textFieldShouldReturn:)]) {
        return [delegate textFieldShouldReturn:textField];
    }
    [textField resignFirstResponder];
    return YES;
}
 
- (void)setDelegate:(id<UITextFieldDelegate>)dele {
    delegate = dele;
}
 
@end
 
@interface LCTextField ()
{
    LCTextFieldSupport *support;          //抽象代理类对象
    NSString *blankMsg;                   //空内容提示
    NSString *strRegExCustom;             //自定义正则表达式
    NSMutableArray *arrRegEx;             //RegEx校验规则键值对数组, 用来存储外部自定义的正则表达式及其校验反馈的字符串
}
 
@property (nonatomic, copy) LCTextFieldMsgBlock blankMsgBlock;
@property (nonatomic, copy) LCTextFieldMsgBlock checkMsgBlock;
@property (nonatomic, copy) LCTextFieldInputBlock result;
 
@end
 
@implementation LCTextField
@synthesize /*isMenuItemsEnable, */ isRuled, disableEmoji, disableSpace, disableSpecialChar, disableUnderRegEx, isChecking, checkOnResign, checkOnTextChanged, chineseStrLength, isSetOneTimeCode;
 
#pragma mark - Default Methods of UITextField
 
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setUp];
    }
    return self;
}
 
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self setUp];
    }
    return self;
}
 
- (void)setInputingBlock:(LCTextFieldInputingBlock)inputingBlock{
    self.delegate = self;
    _inputingBlock = inputingBlock;
}
 
//初始化
- (void)setUp {
    //初始化默认限制属性
    //isMenuItemsEnable = YES; //可长按编辑
    isRuled = NO;     //不限制输入
    disableEmoji = YES;     //Emoji禁用
    disableSpace = NO;     //空白符可用
    disableSpecialChar = NO;     //特殊符号可用
    chineseStrLength = 2;
    //初始化默认校验属性
    arrRegEx = [NSMutableArray new];
    isChecking = NO;     //不校验输入
    checkOnResign = NO;     //不在结束编辑时校验
    checkOnTextChanged = NO;     //不在输入过程中校验
    blankMsg = [LCTextFieldDefaultBlankMsg copy];     //默认空内容提示
    //初始化封装代理类对象
    support = [LCTextFieldSupport new];
    self.customClearButton = YES;
 
    //iOS12自动填充短信内容
    if (@available(iOS 12.0, *)) {
        self.textContentType = UITextContentTypeOneTimeCode;
    }
 
    //设置监听
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldChanged:) name:UITextFieldTextDidChangeNotification object:nil];
}
- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
 
- (void)setDelegate:(id<UITextFieldDelegate>)delegate {
    //1.外部设置的代理(如视图控制器),传入私有类对象support的属性delegate.
    support.delegate = delegate;
    //2.super即UITextField的代理是support
    super.delegate = support;
}
 
- (void)setCheckOnResign:(BOOL)onResign {
    support.checkOnResign = onResign;
    checkOnResign = onResign;
}
 
- (void)setCheckOnTextChanged:(BOOL)onTextChanged {
    support.checkOnTextChanged = onTextChanged;
    checkOnTextChanged = onTextChanged;
}
 
- (void)setIsRuled:(BOOL)isRuledOrNot {
    isRuled = isRuledOrNot;
    disableUnderRegEx = isRuled;
}
 
- (void)setIsSetOneTimeCode:(BOOL)isSetOneTimeCodeOpen {
    isSetOneTimeCode = isSetOneTimeCodeOpen;
 
    //iOS12自动填充短信内容
    if (@available(iOS 12.0, *)) {
        if (isSetOneTimeCode == true) {
            self.textContentType = UITextContentTypeOneTimeCode;
        } else {
            self.textContentType = UITextContentTypeName;
        }
    }
}
 
- (void)textFieldChanged:(NSNotification *)notification {
    //文本输入变化时, 强制限制输入. 默认为NO
    if (isRuled == NO) {
        //密码输入框,进入时,直接回删处理【失去焦点后,重新进入】
        if (notification.object != self) {
            return;
        }
 
        LCTextField *textFiled = (LCTextField *)notification.object;
        NSInteger length = textFiled.text.length;
 
        if (self.secureTextEntry && length == 0 && [self.delegate respondsToSelector:@selector(textFieldShouldClear:)]) {
            [self.delegate textFieldShouldClear:self];
        }
 
        if (self.textChanged) {
            self.textChanged(self.text);
        }
 
        return;
    }
 
    //获取过滤后的内容
    NSString *result = self.text;
    //是否支持emoji表情\空白符\特殊符号
    if (disableEmoji) {
        result = [LCTextField disableEmojiInString:self.text];
    }
    if (disableSpace) {
        result = [LCTextField disableSpaceInString:self.text];
    }
    if (disableSpecialChar) {
        result = [LCTextField disableSpecialCharInString:self.text];
    }
 
    //开启限制isRuled后, 默认开启正则表达式限制
    NSRange selRange = self.selectedRange;
    UITextRange *selectedRange = [self markedTextRange];
    UITextPosition *position = nil;
 
    if (selectedRange) {
        position = [self positionFromPosition:selectedRange.start offset:0];
    }
 
    if (!position /*|| !selectedRange*/) {
        unsigned int strLength = 0;
        for (unsigned int i = 0; i < result.length; i++) {
            if (strRegExCustom.length > 0) {
                NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", strRegExCustom];
                NSString *tmpChar = nil;
                NSRange range;
                range.location = i;
                range.length = 1;
                tmpChar = [result substringWithRange:range];
                if (tmpChar.length > 0 && ![pred evaluateWithObject:tmpChar]) {
                    result = [result stringByReplacingCharactersInRange:range withString:@""];
                    i--;
                } else {
                    unichar uc = [result characterAtIndex:i];
                    NSUInteger customChineseLength = 2;
                    if (chineseStrLength != 2) {
                        customChineseLength = chineseStrLength;
                    }
                    strLength += (isascii(uc) ? 1 : customChineseLength);
                }
            } else {
                unichar uc = [result characterAtIndex:i];
                NSUInteger customChineseLength = 2;
                if (chineseStrLength != 2) {
                    customChineseLength = chineseStrLength;
                }
                strLength += (isascii(uc) ? 1 : customChineseLength);
            }
 
            if (self.strLengthCustom != 0) {
                if (strLength > self.strLengthCustom) {
                    self.text = [result substringToIndex:i];
                    break;
                } else {
                    self.text = result;
                }
            }
        }
    }
 
    if (self.text.length > selRange.location) {
        [self setSelectedRange:NSMakeRange(selRange.location, 0)];
    }
 
    //密码输入框,进入时,直接回删处理【失去焦点后,重新进入】
    if (notification.object != self) {
        return;
    }
 
    LCTextField *textFiled = (LCTextField *)notification.object;
    NSInteger length = textFiled.text.length;
 
    if (self.secureTextEntry && length == 0 && [self.delegate respondsToSelector:@selector(textFieldShouldClear:)]) {
        [self.delegate textFieldShouldClear:self];
    }
 
    if (self.textChanged) {
        self.textChanged(self.text);
    }
}
 
//- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
//{
//    if (self.menuType == LCTextFieldMenuTypeCanNone) {
//        UIMenuController *menuController = [UIMenuController sharedMenuController];
//        if (menuController) {
//            [UIMenuController sharedMenuController].menuVisible = NO;
//        }
//        return NO;
//    }
//    if (self.menuType == LCTextFieldMenuTypeCanCopy && action == @selector(copy:)) {
//        return YES;
//    }
//    if (self.menuType == LCTextFieldMenuTypeCanSelect && action == @selector(select:)) {
//        return YES;
//    }
//    if (self.menuType == LCTextFieldMenuTypeCanSelectAll && action == @selector(selectAll:)) {
//        return YES;
//    }
//    if (self.menuType == LCTextFieldMenuTypeCanPaste && action == @selector(paste:)) {
//        return YES;
//    }
//
//    return NO;
//}
 
//过滤emoji
+ (NSString *)disableEmojiInString:(NSString *)text {
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:EMOJI options:NSRegularExpressionCaseInsensitive error:nil];
    NSString *modifiedString = [regex stringByReplacingMatchesInString:text
                                                               options:0
                                                                 range:NSMakeRange(0, [text length])
                                                          withTemplate:@""];
    return modifiedString;
}
 
//过滤空白符
+ (NSString *)disableSpaceInString:(NSString *)text {
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:SPACE options:NSRegularExpressionCaseInsensitive error:nil];
    NSString *modifiedString = [regex stringByReplacingMatchesInString:text
                                                               options:0
                                                                 range:NSMakeRange(0, [text length])
                                                          withTemplate:@""];
    return modifiedString;
}
 
//过滤特殊符号
+ (NSString *)disableSpecialCharInString:(NSString *)text {
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:LC_ILLEGALCHAR options:NSRegularExpressionCaseInsensitive error:nil];
    NSString *modifiedString = [regex stringByReplacingMatchesInString:text
                                                               options:0
                                                                 range:NSMakeRange(0, [text length])
                                                          withTemplate:@""];
    return modifiedString;
}
 
#pragma mark -
- (void)delete:(id)sender
{
    self.text = @"";
}
 
#pragma mark - Public Methods - Rule
 
- (void)lc_setInputRuleWithRegEx:(NSString *)string andInputLength:(unsigned int)Length {
    strRegExCustom = string;
    self.strLengthCustom = Length;
    self.isRuled = YES;
}
 
- (void)lc_setBlankMsgBlock:(LCTextFieldMsgBlock)block {
    self.blankMsgBlock = block;
}
 
- (void)lc_setCheckMsgBlock:(LCTextFieldMsgBlock)block {
    self.checkMsgBlock = block;
}
 
#pragma mark - Public Methods - Check
//增加校验规则
 
- (void)lc_addRegExToCheckTextField:(NSString *)strRegEx withMsgBlock:(LCTextFieldMsgBlock)msgBlock {
    isChecking = YES;
    self.checkMsgBlock = msgBlock;
    NSDictionary *dic = [[NSDictionary alloc] initWithObjectsAndKeys:strRegEx, @"RegEx", self.checkMsgBlock, @"Msg", nil];
    [arrRegEx addObject:dic];
}
 
//设置确认密码的目标输入框
- (void)lc_addConfirmValidationToCheckTextField:(LCTextField *)targetTextField withMsgBlock:(LCTextFieldMsgBlock)msgBlock {
    isChecking = YES;
    self.checkMsgBlock = msgBlock;
    NSDictionary *dic = [[NSDictionary alloc] initWithObjectsAndKeys:targetTextField, @"confirm", self.checkMsgBlock, @"Msg", nil];
    [arrRegEx addObject:dic];
}
 
//正则表达式匹配
- (BOOL)checkString:(NSString *)stringToCheck withRegEx:(NSString *)strRegEx {
    NSPredicate *regex = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", strRegEx];
    return [regex evaluateWithObject:stringToCheck];
}
 
- (BOOL)lc_checkIt {
    //校验开关
    if (isChecking) {
        //校验是否为空, 空返回NO;
        if ([self.text length] == 0) {
            NSLog(@"%@", blankMsg);
            if (self.blankMsgBlock != NULL) {
                self.blankMsgBlock(blankMsg);
            }
            return NO;
        }
        //按照规则校验
        for (int i = 0; i < [arrRegEx count]; i++) {
            if (self.checkMsgBlock == NULL) {
                return NO;
            }
            NSDictionary *dic = [arrRegEx objectAtIndex:i];
            if ([dic objectForKey:@"confirm"]) {
                LCTextField *txtConfirm = [dic objectForKey:@"confirm"];
                if (![txtConfirm.text isEqualToString:self.text]) {
                    self.checkMsgBlock([dic objectForKey:@"Msg"]);
                    return NO;
                }
            } else if (![[dic objectForKey:@"RegEx"] isEqualToString:@""] && [self.text length] != 0 && ![self checkString:self.text withRegEx:[dic objectForKey:@"RegEx"]]) {
                self.checkMsgBlock([dic objectForKey:@"Msg"]);
                return NO;
            }
        }
    }
    return YES;
}
 
#pragma mark - PlaceHolder(由于全局设置字体后,导致中文字初始位置偏低)
- (void)setPlaceholder:(NSString *)placeholder
{
    [super setPlaceholder:placeholder];
    UIFont *originFont = self.font;
    self.font = nil;
    self.font = originFont;
}
 
//- (CGRect)placeholderRectForBounds:(CGRect)bounds
//{
//    
//    UIFont *font = self.font ? : [UIFont dhFont_f1];
//    CGSize size = [self.placeholder sizeWithAttributes: @{NSFontAttributeName: font}];
//    CGRect placeHolderFrame = [super placeholderRectForBounds:bounds];
//    
//    //Placeholder居中显示
//    if (self.textAlignment == NSTextAlignmentCenter) {
//        placeHolderFrame = CGRectMake((bounds.size.width - size.width) / 2 + _textOffsetWhenInAlignmentCenter, (bounds.size.height - size.height) / 2, size.width, size.height);
//    }
//    
//    return placeHolderFrame;
//}
 
- (CGRect)textRectForBounds:(CGRect)bounds{
    CGRect inset = [super textRectForBounds:bounds];
    if (self.textAlignment == NSTextAlignmentCenter) {
        inset = CGRectMake(bounds.origin.x + _textOffsetWhenInAlignmentCenter, bounds.origin.y, bounds.size.width, bounds.size.height);
    }
 
    return inset;
}
 
+ (instancetype)lcTextFieldWithResult:(void (^)(NSString *result))result {
    LCTextField *textField = [LCTextField new];
    textField.result = result;
    return textField;
}
 
 
@end