-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathInput_OnlyText_Cell.m
executable file
·308 lines (276 loc) · 13.6 KB
/
Input_OnlyText_Cell.m
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
//
// Input_OnlyText_Cell.m
// Coding_iOS
//
// Created by 王 原闯 on 14-8-4.
// Copyright (c) 2014年 Coding. All rights reserved.
//
#define kCellIdentifier_Input_OnlyText_Cell_PhoneCode_Prefix @"Input_OnlyText_Cell_PhoneCode"
#import "Input_OnlyText_Cell.h"
#import "Coding_NetAPIManager.h"
@interface Input_OnlyText_Cell ()
@property (strong, nonatomic) UIView *lineView;
@property (strong, nonatomic) UIButton *clearBtn, *passwordBtn;
@property (strong, nonatomic) UITapImageView *captchaView;
@property (strong, nonatomic) UIActivityIndicatorView *activityIndicator;
@end
@implementation Input_OnlyText_Cell
+ (NSString *)randomCellIdentifierOfPhoneCodeType{
return [NSString stringWithFormat:@"%@_%ld", kCellIdentifier_Input_OnlyText_Cell_PhoneCode_Prefix, random()];
}
- (void)setIsBottomLineShow:(BOOL)isBottomLineShow{
_isBottomLineShow = isBottomLineShow;
_lineView.hidden = !_isBottomLineShow;
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
self.clipsToBounds = YES;
self.selectionStyle = UITableViewCellSelectionStyleNone;
if (!_textField) {
_textField = [UITextField new];
[_textField setFont:[UIFont systemFontOfSize:15]];
[_textField addTarget:self action:@selector(editDidBegin:) forControlEvents:UIControlEventEditingDidBegin];
[_textField addTarget:self action:@selector(textValueChanged:) forControlEvents:UIControlEventEditingChanged];
[_textField addTarget:self action:@selector(editDidEnd:) forControlEvents:UIControlEventEditingDidEnd];
[self.contentView addSubview:_textField];
[_textField mas_makeConstraints:^(MASConstraintMaker *make) {
make.height.mas_equalTo(20);
make.left.equalTo(self.contentView).offset(kLoginPaddingLeftWidth);
make.right.equalTo(self.contentView).offset(-kLoginPaddingLeftWidth);
make.bottom.mas_greaterThanOrEqualTo(self.contentView).offset(-15).priority(MASLayoutPriorityRequired);
make.centerY.equalTo(self.contentView).priority(MASLayoutPriorityDefaultLow);
}];
}
if ([reuseIdentifier isEqualToString:kCellIdentifier_Input_OnlyText_Cell_Text]) {
}else if ([reuseIdentifier isEqualToString:kCellIdentifier_Input_OnlyText_Cell_Captcha]){
__weak typeof(self) weakSelf = self;
if (!_captchaView) {
_captchaView = [[UITapImageView alloc] initWithFrame:CGRectMake(kScreen_Width - 60 - kLoginPaddingLeftWidth, (44-25)/2, 60, 25)];
_captchaView.layer.masksToBounds = YES;
_captchaView.layer.cornerRadius = 5;
[_captchaView addTapBlock:^(id obj) {
[weakSelf refreshCaptchaImage];
}];
[self.contentView addSubview:_captchaView];
[_captchaView mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(60, 25));
make.centerY.equalTo(self.textField);
make.right.equalTo(self.contentView).offset(-kLoginPaddingLeftWidth);
}];
}
if (!_activityIndicator) {
_activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
_activityIndicator.hidesWhenStopped = YES;
[self.contentView addSubview:_activityIndicator];
[_activityIndicator mas_makeConstraints:^(MASConstraintMaker *make) {
make.center.equalTo(self.captchaView);
}];
}
}else if ([reuseIdentifier isEqualToString:kCellIdentifier_Input_OnlyText_Cell_Password]){
if (!_passwordBtn) {
_textField.secureTextEntry = YES;
_passwordBtn = [[UIButton alloc] initWithFrame:CGRectMake(kScreen_Width - 44- kLoginPaddingLeftWidth, 0, 44, 44)];
[_passwordBtn setImage:[UIImage imageNamed:@"password_unlook"] forState:UIControlStateNormal];
[_passwordBtn addTarget:self action:@selector(passwordBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.contentView addSubview:_passwordBtn];
}
}else if ([reuseIdentifier hasPrefix:kCellIdentifier_Input_OnlyText_Cell_PhoneCode_Prefix]){
if (!_verify_codeBtn) {
_verify_codeBtn = [[PhoneCodeButton alloc] initWithFrame:CGRectMake(kScreen_Width - 80 - kLoginPaddingLeftWidth, (44-25)/2, 80, 25)];
[_verify_codeBtn addTarget:self action:@selector(phoneCodeButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.contentView addSubview:_verify_codeBtn];
[_verify_codeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(80, 25));
make.centerY.equalTo(self.textField);
make.right.equalTo(self.contentView).offset(-kLoginPaddingLeftWidth);
}];
}
}else if ([reuseIdentifier isEqualToString:kCellIdentifier_Input_OnlyText_Cell_Company]){
if (!_companySuffixL) {
_companySuffixL = [UILabel labelWithFont:[UIFont systemFontOfSize:17] textColor:kColor222];
[self.contentView addSubview:_companySuffixL];
[_companySuffixL mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.equalTo(self.textField);
make.right.equalTo(self.contentView).offset(-kPaddingLeftWidth);
}];
{
UIView *splitLineV = [UIView new];
splitLineV.backgroundColor = kColorDDD;
[self.contentView addSubview:splitLineV];
[splitLineV mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.equalTo(self.textField);
make.size.mas_equalTo(CGSizeMake(1.0/[UIScreen mainScreen].scale, 20));
make.right.equalTo(self.companySuffixL.mas_left).offset(-10);
}];
}
}
}else if ([reuseIdentifier isEqualToString:kCellIdentifier_Input_OnlyText_Cell_Phone]){
_countryCodeL = ({
UILabel *label = [UILabel new];
label.font = [UIFont systemFontOfSize:15];
label.textColor = kColorBrandBlue;
[self.contentView addSubview:label];
[label mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.contentView).offset(kPaddingLeftWidth);
make.centerY.equalTo(self.textField);
}];
label;
});
UIView *lineV = ({
UIView *view = [UIView new];
view.backgroundColor = kColorCCC;
[self.contentView addSubview:view];
[view mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.countryCodeL.mas_right).offset(8);
make.centerY.equalTo(self.countryCodeL);
make.width.mas_offset(0.5);
make.height.mas_equalTo(15.0);
}];
view;
});
UIButton *bgBtn = ({
UIButton *button = [UIButton new];
[self.contentView addSubview:button];
[button mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.top.bottom.equalTo(self.contentView);
make.right.equalTo(lineV);
}];
button;
});
[bgBtn addTarget:self action:@selector(countryCodeBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
[_textField mas_remakeConstraints:^(MASConstraintMaker *make) {
make.height.mas_equalTo(20);
make.right.equalTo(self.contentView).offset(-kLoginPaddingLeftWidth);
make.left.equalTo(lineV.mas_right).offset(8.0);
make.bottom.mas_greaterThanOrEqualTo(self.contentView).offset(-15).priority(MASLayoutPriorityRequired);
make.centerY.equalTo(self.contentView).priority(MASLayoutPriorityDefaultLow);
}];
}
}
return self;
}
- (void)prepareForReuse{
[super prepareForReuse];
self.isBottomLineShow = NO;
if (![self.reuseIdentifier isEqualToString:kCellIdentifier_Input_OnlyText_Cell_Password]) {
self.textField.secureTextEntry = NO;
}
self.textField.userInteractionEnabled = YES;
self.textField.keyboardType = UIKeyboardTypeDefault;
self.editDidBeginBlock = nil;
self.textValueChangedBlock = nil;
self.editDidEndBlock = nil;
self.phoneCodeBtnClckedBlock = nil;
}
- (void)setPlaceholder:(NSString *)phStr value:(NSString *)valueStr{
self.textField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:phStr? phStr: @"" attributes:@{NSForegroundColorAttributeName: kColorDarkA}];
self.textField.text = valueStr;
}
#pragma mark Button
- (void)clearBtnClicked:(id)sender {
self.textField.text = @"";
[self textValueChanged:nil];
}
- (void)phoneCodeButtonClicked:(id)sender{
if (self.phoneCodeBtnClckedBlock) {
self.phoneCodeBtnClckedBlock(sender);
}
}
- (void)countryCodeBtnClicked:(id)sender{
if (_countryCodeBtnClickedBlock) {
_countryCodeBtnClickedBlock();
}
}
#pragma mark - UIView
- (void)layoutSubviews {
[super layoutSubviews];
if (!_lineView && _isBottomLineShow) {
_lineView = [UIView new];
if (kTarget_Enterprise) {
_lineView.backgroundColor = [UIColor colorWithHexString:@"0xD8DDE4"];
}else{
_lineView.backgroundColor = kColorDarkA;
}
[self.contentView addSubview:_lineView];
[_lineView mas_makeConstraints:^(MASConstraintMaker *make) {
make.height.mas_equalTo(kLine_MinHeight);
make.left.equalTo(self.contentView).offset(kLoginPaddingLeftWidth);
make.right.equalTo(self.contentView).offset(-kLoginPaddingLeftWidth);
make.bottom.equalTo(self.contentView);
}];
}
self.backgroundColor = [UIColor whiteColor];
self.textField.clearButtonMode = UITextFieldViewModeWhileEditing;
self.textField.textColor = kColorDark2;
self.clearBtn.hidden = YES;
UIView *rightElement;
if ([self.reuseIdentifier isEqualToString:kCellIdentifier_Input_OnlyText_Cell_Text]) {
rightElement = nil;
}else if ([self.reuseIdentifier isEqualToString:kCellIdentifier_Input_OnlyText_Cell_Captcha]){
rightElement = _captchaView;
[self refreshCaptchaImage];
}else if ([self.reuseIdentifier isEqualToString:kCellIdentifier_Input_OnlyText_Cell_Password]){
rightElement = _passwordBtn;
}else if ([self.reuseIdentifier hasPrefix:kCellIdentifier_Input_OnlyText_Cell_PhoneCode_Prefix]){
rightElement = _verify_codeBtn;
}else if ([self.reuseIdentifier isEqualToString:kCellIdentifier_Input_OnlyText_Cell_Company]){
rightElement = _companySuffixL;
}
[_clearBtn mas_updateConstraints:^(MASConstraintMaker *make) {
CGFloat offset = rightElement? (CGRectGetMinX(rightElement.frame) - kScreen_Width - 10): -kLoginPaddingLeftWidth;
make.right.equalTo(self.contentView).offset(offset);
}];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{//layout 的时候,rightElement 的 frame 还没有固定
[_textField mas_updateConstraints:^(MASConstraintMaker *make) {
CGFloat offset = rightElement? (CGRectGetMinX(rightElement.frame) - kScreen_Width - 10): -kLoginPaddingLeftWidth;
make.right.equalTo(self.contentView).offset(offset);
}];
});
}
#pragma password
- (void)passwordBtnClicked:(UIButton *)button{
_textField.secureTextEntry = !_textField.secureTextEntry;
[button setImage:[UIImage imageNamed:_textField.secureTextEntry? @"password_unlook": @"password_look"] forState:UIControlStateNormal];
}
#pragma Captcha
- (void)refreshCaptchaImage{
__weak typeof(self) weakSelf = self;
if (_activityIndicator.isAnimating) {
return;
}
[_activityIndicator startAnimating];
[self.captchaView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@api/getCaptcha", [NSObject baseURLStr]]] placeholderImage:nil options:(SDWebImageRetryFailed | SDWebImageRefreshCached | SDWebImageHandleCookies) completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
[weakSelf.activityIndicator stopAnimating];
}];
}
#pragma mark TextField
- (void)editDidBegin:(id)sender {
if (kTarget_Enterprise) {
self.lineView.backgroundColor = [UIColor colorWithHexString:@"0x323A45"];
}else{
self.lineView.backgroundColor = kColorBrandBlue;
}
if (self.editDidBeginBlock) {
self.editDidBeginBlock(self.textField.text);
}
}
- (void)editDidEnd:(id)sender {
if (kTarget_Enterprise) {
self.lineView.backgroundColor = [UIColor colorWithHexString:@"0xD8DDE4"];
}else{
self.lineView.backgroundColor = kColorDarkA;
}
self.clearBtn.hidden = YES;
if (self.editDidEndBlock) {
self.editDidEndBlock(self.textField.text);
}
}
- (void)textValueChanged:(id)sender {
if (self.textValueChangedBlock) {
self.textValueChangedBlock(self.textField.text);
}
}
@end