JLChen
2021-04-30 a5247b61d585627a1a7b1e1f35f34de9f0af9fba
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
//
//  Copyright © 2020 jm. All rights reserved.
//
 
#import <LCBaseModule/DHTextField.h>
#import <LCBaseModule/UITextField+LeChange.h>
#import <LCBaseModule/UIColor+LeChange.h>
#import <LCBaseModule/UIFont+Dahua.h>
 
@interface DHTextField ()<UITextFieldDelegate>
 
/// result
@property (nonatomic, copy) DHTextFieldInputBlock result;
/// 反馈结果
@property (strong, nonatomic) NSString *resultString;
 
@end
 
 
@implementation DHTextField
 
@synthesize placeholder = _placeholder;
 
+ (instancetype)lcTextFieldWithResult:(void (^)(NSString *result))result {
    DHTextField *textField = [DHTextField new];
    textField.result = result;
    return textField;
}
- (void)setInputingBlock:(DHTextFieldInputingBlock)inputingBlock{
    self.delegate = self;
    _inputingBlock = inputingBlock;
}
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.font = [UIFont dhFont_t4];
        self.delegate = self;
    }
    return self;
}
 
// 未输入时文本的位置,向右缩进10
- (CGRect)textRectForBounds:(CGRect)bounds {
    return CGRectInset(bounds, 10, 0);
}
 
// 输入后文本的位置,向右缩进10
- (CGRect)editingRectForBounds:(CGRect)bounds {
    return CGRectInset(bounds, 10, 0);
}
 
- (void)setPlaceholder:(NSString *)placeholder {
    self.attributedPlaceholder = [[NSAttributedString alloc]initWithString:placeholder attributes:@{ NSFontAttributeName: [UIFont dhFont_t4], NSForegroundColorAttributeName: [UIColor dhcolor_c41] }];
}
-(BOOL)textFieldShouldClear:(UITextField *)textField{
    if (self.result) {
        self.result(@"");
    }
    return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField reason:(UITextFieldDidEndEditingReason)reason {
    if (self.result) {
        self.result(textField.text);
    }
}
 
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    NSLog(@"%@--------%@", string, textField.text);
    if (self.inputingBlock) {
       return self.inputingBlock(textField.text,string);
    }
    if ([self hasEmoji:string]) {
        return NO;
    }
 
    return YES;
}
-(BOOL)textFieldShouldEndEditing:(UITextField *)textField{
    NSLog(@"-++++++----%@", textField.text);
    return YES;
}
/**
 *  判断字符串中是否存在emoji
 * @param string 字符串
 * @return YES(含有表情)
 */
- (BOOL)hasEmoji:(NSString *)string;
{
    NSString *pattern = @"[^\\u0020-\\u007E\\u00A0-\\u00BE\\u2E80-\\uA4CF\\uF900-\\uFAFF\\uFE30-\\uFE4F\\uFF00-\\uFFEF\\u0080-\\u009F\\u2000-\\u201f\r\n]";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];
    BOOL isMatch = [pred evaluateWithObject:string];
    return isMatch;
}
 
/**
 判断是否含有中文
 
 @param str 字符串
 @return YES 含有中文
 */
- (BOOL)isChinese:(NSString *)str
{
    NSString *match = @"(^[\u4e00-\u9fa5]+$)";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF matches %@", match];
    return [predicate evaluateWithObject:str];
}
 
 
 
@end