Skip to content

Commit deda7a8

Browse files
committed
add Finish Status
1 parent ac217e5 commit deda7a8

File tree

8 files changed

+157
-51
lines changed

8 files changed

+157
-51
lines changed

.DS_Store

-6 KB
Binary file not shown.

JHVerificationCodeView.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Pod::Spec.new do |s|
33
s.name = "JHVerificationCodeView"
4-
s.version = "1.3.5"
4+
s.version = "1.3.6"
55
s.summary = "A simple Verification Code View."
66
s.homepage = "https://github.com/xjh093/JHVerificationCodeView"
77
s.license = "MIT"

JHVerificationCodeView.xcodeproj/project.pbxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@
188188
developmentRegion = English;
189189
hasScannedForEncodings = 0;
190190
knownRegions = (
191+
English,
191192
en,
192193
Base,
193194
);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<Bucket
3+
uuid = "5E172D97-7924-44AA-85D1-5D1DE43E5BDC"
34
type = "1"
45
version = "2.0">
56
</Bucket>

JHVerificationCodeView/JHVerificationCodeView/JHVerificationCodeView.h

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2828
// SOFTWARE.
2929

30+
// version: 1.3.6
31+
// date: 2019-12-24
32+
3033
#import <UIKit/UIKit.h>
3134

3235
typedef NS_ENUM(NSUInteger, JHVCConfigInputType) {
@@ -36,6 +39,9 @@ typedef NS_ENUM(NSUInteger, JHVCConfigInputType) {
3639
};
3740

3841
@interface JHVCConfig : NSObject
42+
43+
//============================ Initialization ============================
44+
3945
///输入框个数
4046
@property (assign, nonatomic) NSInteger inputBoxNumber;
4147
///单个输入框的宽度
@@ -52,15 +58,13 @@ typedef NS_ENUM(NSUInteger, JHVCConfigInputType) {
5258
@property (assign, nonatomic) CGFloat leftMargin;
5359
///单个输入框的颜色, Default is lightGrayColor
5460
@property (strong, nonatomic) UIColor *inputBoxColor;
55-
///单个输入框输入时的颜色
56-
@property (strong, nonatomic) UIColor *inputBoxHighlightedColor;
5761
///光标颜色, Default is blueColor
5862
@property (strong, nonatomic) UIColor *tintColor;
5963
///显示 或 隐藏
6064
@property (assign, nonatomic) BOOL secureTextEntry;
61-
///字体
65+
///字体, Default is [UIFont boldSystemFontOfSize:16]
6266
@property (strong, nonatomic) UIFont *font;
63-
///颜色
67+
///颜色, Default is [UIColor blackColor]
6468
@property (strong, nonatomic) UIColor *textColor;
6569
///输入类型:数字+字母,数字,字母. Default is 'JHVCConfigInputType_Number_Alphabet'
6670
@property (nonatomic, assign) JHVCConfigInputType inputType;
@@ -74,20 +78,38 @@ typedef NS_ENUM(NSUInteger, JHVCConfigInputType) {
7478
@property (nonatomic, assign) CGSize underLineSize;
7579
///下划线颜色, Default is lightGrayColor
7680
@property (nonatomic, strong) UIColor *underLineColor;
77-
///下划线高亮颜色
78-
@property (nonatomic, strong) UIColor *underLineHighlightedColor;
7981
///自定义的输入占位字符,secureTextEntry = NO,有效
8082
@property (nonatomic, copy) NSString *customInputHolder;
8183
///设置键盘类型
8284
@property (nonatomic, assign) UIKeyboardType keyboardType;
8385
///使用系统的密码键盘
8486
@property (nonatomic, assign) BOOL useSystemPasswordKeyboard;
87+
88+
//============================ Input ============================
89+
90+
///单个输入框输入时的颜色
91+
@property (strong, nonatomic) UIColor *inputBoxHighlightedColor;
92+
///下划线高亮颜色
93+
@property (nonatomic, strong) UIColor *underLineHighlightedColor;
94+
95+
//============================ Finish ============================
96+
/* 输入完成后,可能根据不同的状态,显示不同的颜色。 */
97+
98+
///单个输入框输入时的颜色
99+
@property (strong, nonatomic) NSArray<UIColor*> *inputBoxFinishColors;
100+
///下划线高亮颜色
101+
@property (nonatomic, strong) NSArray<UIColor*> *underLineFinishColors;
102+
///输入完成时字体
103+
@property (strong, nonatomic) NSArray<UIFont*> *finishFonts;
104+
///输入完成时颜色
105+
@property (strong, nonatomic) NSArray<UIColor*> *finishTextColors;
106+
85107
@end
86108

87109
@interface JHVerificationCodeView : UIView
88110

89111
@property (copy, nonatomic) void (^inputBlock)(NSString *code);
90-
@property (copy, nonatomic) void (^finishBlock)(NSString *code);
112+
@property (copy, nonatomic) void (^finishBlock)(JHVerificationCodeView *codeView, NSString *code);
91113

92114
- (instancetype)initWithFrame:(CGRect)frame config:(JHVCConfig *)config;
93115

@@ -96,4 +118,11 @@ typedef NS_ENUM(NSUInteger, JHVCConfigInputType) {
96118
*/
97119
- (void)clear;
98120

121+
/**
122+
输入完成后,调用此方法,根据 index 从 `Finish` 下的4个属性获取对应的值来设置颜色。
123+
124+
@param index 用于从 `inputBoxFinishColors`、`underLineFinishColors`、`finishFonts`、`finishTextColors`中获取对应的值
125+
*/
126+
- (void)showInputFinishColorWithIndex:(NSUInteger)index;
127+
99128
@end

JHVerificationCodeView/JHVerificationCodeView/JHVerificationCodeView.m

Lines changed: 89 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ - (instancetype)init{
3939
_inputBoxSpacing = 5;
4040
_inputBoxColor = [UIColor lightGrayColor];
4141
_tintColor = [UIColor blueColor];
42+
_font = [UIFont boldSystemFontOfSize:16];
43+
_textColor = [UIColor blackColor];
4244
_showFlickerAnimation = YES;
4345
_underLineColor = [UIColor lightGrayColor];
4446
}
@@ -50,6 +52,8 @@ - (instancetype)init{
5052
@interface JHVerificationCodeView()
5153
@property (strong, nonatomic) JHVCConfig *config;
5254
@property (strong, nonatomic) UITextView *textView;
55+
@property (nonatomic, assign) BOOL inputFinish;
56+
@property (nonatomic, assign) NSUInteger inputFinishIndex;
5357
@end
5458

5559
@implementation JHVerificationCodeView
@@ -226,26 +230,8 @@ - (void)xx_textChange:(NSNotification *)noti
226230
return;
227231
}
228232

229-
NSInteger count = _config.inputBoxNumber;
230-
231233
// set default
232-
for (int i = 0; i < count; ++i) {
233-
UITextField *textField = self.subviews[i];
234-
textField.text = @"";
235-
236-
if (_config.inputBoxColor) {
237-
textField.layer.borderColor = _config.inputBoxColor.CGColor;
238-
}
239-
if (_config.showFlickerAnimation) {
240-
CALayer *layer = textField.layer.sublayers[0];
241-
layer.hidden = YES;
242-
[layer removeAnimationForKey:kFlickerAnimation];
243-
}
244-
if (_config.showUnderLine) {
245-
UIView *underLine = [textField viewWithTag:100];
246-
underLine.backgroundColor = _config.underLineColor;
247-
}
248-
}
234+
[self xx_setDefault];
249235

250236
// trim space
251237
NSString *text = [_textView.text stringByReplacingOccurrencesOfString:@" " withString:@""];
@@ -273,6 +259,7 @@ - (void)xx_textChange:(NSNotification *)noti
273259
}
274260

275261
text = mstr;
262+
NSInteger count = _config.inputBoxNumber;
276263
if (text.length > count) {
277264
text = [text substringToIndex:count];
278265
}
@@ -281,44 +268,99 @@ - (void)xx_textChange:(NSNotification *)noti
281268
_inputBlock(text);
282269
}
283270

284-
//NSLog(@"%@",text);
285-
286271
// set value
287-
for (int i = 0; i < text.length; ++i) {
288-
unichar c = [text characterAtIndex:i];
272+
[self xx_setValue:text];
273+
274+
// Flicker Animation
275+
[self xx_flickerAnimation:text];
276+
277+
if (_inputFinish) {
278+
[self xx_finish];
279+
}
280+
}
281+
282+
- (void)xx_setDefault
283+
{
284+
for (int i = 0; i < _config.inputBoxNumber; ++i) {
289285
UITextField *textField = self.subviews[i];
290-
textField.text = [NSString stringWithFormat:@"%c",c];
291-
if (!textField.secureTextEntry && _config.customInputHolder.length > 0) {
292-
textField.text = _config.customInputHolder;
293-
}
286+
textField.text = @"";
294287

295-
if (_config.inputBoxHighlightedColor) {
296-
textField.layer.borderColor = _config.inputBoxHighlightedColor.CGColor;
288+
if (_config.inputBoxColor) {
289+
textField.layer.borderColor = _config.inputBoxColor.CGColor;
290+
}
291+
if (_config.showFlickerAnimation) {
292+
CALayer *layer = textField.layer.sublayers[0];
293+
layer.hidden = YES;
294+
[layer removeAnimationForKey:kFlickerAnimation];
297295
}
298-
299-
if (_config.showUnderLine && _config.underLineHighlightedColor) {
296+
if (_config.showUnderLine) {
300297
UIView *underLine = [textField viewWithTag:100];
301-
underLine.backgroundColor = _config.underLineHighlightedColor;
298+
underLine.backgroundColor = _config.underLineColor;
302299
}
303300
}
304-
305-
// Flicker Animation
301+
}
302+
303+
- (void)xx_flickerAnimation:(NSString *)text
304+
{
306305
if (_config.showFlickerAnimation && text.length < self.subviews.count) {
307306
UITextField *textField = self.subviews[text.length];
308307
CALayer *layer = textField.layer.sublayers[0];
309308
layer.hidden = NO;
310309
[layer addAnimation:[self xx_alphaAnimation] forKey:kFlickerAnimation];
311310
}
311+
}
312+
313+
- (void)xx_setValue:(NSString *)text
314+
{
315+
_inputFinish = (text.length == _config.inputBoxNumber);
312316

313-
if (text.length == count) {
314-
[self xx_finish];
317+
for (int i = 0; i < text.length; ++i) {
318+
unichar c = [text characterAtIndex:i];
319+
UITextField *textField = self.subviews[i];
320+
textField.text = [NSString stringWithFormat:@"%c",c];
321+
if (!textField.secureTextEntry && _config.customInputHolder.length > 0) {
322+
textField.text = _config.customInputHolder;
323+
}
324+
325+
// Input Status
326+
UIFont *font = _config.font;
327+
UIColor *color = _config.textColor;
328+
UIColor *inputBoxColor = _config.inputBoxHighlightedColor;
329+
UIColor *underLineColor = _config.underLineHighlightedColor;
330+
331+
// Finish Status
332+
if (_inputFinish) {
333+
if (_inputFinishIndex < _config.finishFonts.count) {
334+
font = _config.finishFonts[_inputFinishIndex];
335+
}
336+
if (_inputFinishIndex < _config.finishTextColors.count) {
337+
color = _config.finishTextColors[_inputFinishIndex];
338+
}
339+
if (_inputFinishIndex < _config.inputBoxFinishColors.count) {
340+
inputBoxColor = _config.inputBoxFinishColors[_inputFinishIndex];
341+
}
342+
if (_inputFinishIndex < _config.underLineFinishColors.count) {
343+
underLineColor = _config.underLineFinishColors[_inputFinishIndex];
344+
}
345+
}
346+
347+
textField.font = font;
348+
textField.textColor = color;
349+
350+
if (inputBoxColor) {
351+
textField.layer.borderColor = inputBoxColor.CGColor;
352+
}
353+
if (_config.showUnderLine && underLineColor) {
354+
UIView *underLine = [textField viewWithTag:100];
355+
underLine.backgroundColor = underLineColor;
356+
}
315357
}
316358
}
317359

318360
- (void)xx_finish
319361
{
320362
if (_finishBlock) {
321-
_finishBlock(_textView.text);
363+
_finishBlock(self, _textView.text);
322364
}
323365

324366
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
@@ -331,7 +373,16 @@ - (void)xx_finish
331373
- (void)clear
332374
{
333375
_textView.text = @"";
334-
[[NSNotificationCenter defaultCenter] postNotificationName:UITextViewTextDidChangeNotification object:_textView];
376+
377+
[self xx_setDefault];
378+
[self xx_flickerAnimation:_textView.text];
379+
}
380+
381+
- (void)showInputFinishColorWithIndex:(NSUInteger)index
382+
{
383+
_inputFinishIndex = index;
384+
385+
[self xx_setValue:_textView.text];
335386
}
336387

337388
@end

JHVerificationCodeView/ViewController.m

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,15 @@ - (void)jhSetupViews
6262
config.inputBoxBorderWidth = 1;
6363
config.inputBoxCornerRadius = 5;
6464

65-
config.customInputHolder = @"🔒";
65+
//config.customInputHolder = @"🔒";
6666

6767
config.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
6868
config.useSystemPasswordKeyboard = YES;
6969

70+
config.inputBoxFinishColors = @[[UIColor redColor],[UIColor orangeColor]];
71+
config.finishFonts = @[[UIFont boldSystemFontOfSize:20],[UIFont systemFontOfSize:20]];
72+
config.finishTextColors = @[[UIColor greenColor],[UIColor orangeColor]];
73+
7074
[self.view addSubview:({
7175

7276
UILabel *label = [[UILabel alloc] init];
@@ -77,8 +81,12 @@ - (void)jhSetupViews
7781
JHVerificationCodeView *codeView =
7882
[[JHVerificationCodeView alloc] initWithFrame:CGRectMake(10, 150, kScreenWidth-20, 30)
7983
config:config];
80-
codeView.finishBlock = ^(NSString *code) {
84+
codeView.finishBlock = ^(JHVerificationCodeView *codeView, NSString *code) {
8185
label.text = code;
86+
87+
// 根据最后输入结果,判断显示哪种颜色
88+
NSUInteger index = [code isEqualToString:@"123asd"] ? 1 : 0;
89+
[codeView showInputFinishColorWithIndex:index];
8290
};
8391
codeView.inputBlock = ^(NSString *code) {
8492
NSLog(@"example 1 code:%@",code);
@@ -113,6 +121,10 @@ - (void)jhSetupViews
113121

114122
config.customInputHolder = @"🈲";
115123

124+
config.inputBoxFinishColors = @[[UIColor orangeColor],[UIColor redColor]];
125+
config.finishFonts = @[[UIFont boldSystemFontOfSize:20],[UIFont systemFontOfSize:20]];
126+
config.finishTextColors = @[[UIColor orangeColor],[UIColor greenColor]];
127+
116128
[self.view addSubview:({
117129

118130
UILabel *label = [[UILabel alloc] init];
@@ -123,8 +135,12 @@ - (void)jhSetupViews
123135
JHVerificationCodeView *codeView =
124136
[[JHVerificationCodeView alloc] initWithFrame:CGRectMake(10, 240, kScreenWidth-20, 30)
125137
config:config];
126-
codeView.finishBlock = ^(NSString *code) {
138+
codeView.finishBlock = ^(JHVerificationCodeView *codeView, NSString *code) {
127139
label.text = code;
140+
141+
// 根据最后输入结果,判断显示哪种颜色
142+
NSUInteger index = [code isEqualToString:@"123456"] ? 1 : 0;
143+
[codeView showInputFinishColorWithIndex:index];
128144
};
129145
codeView.inputBlock = ^(NSString *code) {
130146
NSLog(@"example 2 code:%@",code);
@@ -168,7 +184,7 @@ - (void)jhSetupViews
168184
JHVerificationCodeView *codeView =
169185
[[JHVerificationCodeView alloc] initWithFrame:CGRectMake(10, 330, kScreenWidth-20, 30)
170186
config:config];
171-
codeView.finishBlock = ^(NSString *code) {
187+
codeView.finishBlock = ^(JHVerificationCodeView *codeView, NSString *code) {
172188
label.text = code;
173189
};
174190
codeView.inputBlock = ^(NSString *code) {
@@ -205,6 +221,10 @@ - (void)jhSetupViews
205221
config.underLineColor = [UIColor brownColor];
206222
config.underLineHighlightedColor = [UIColor redColor];
207223

224+
config.underLineFinishColors = @[[UIColor blueColor],[UIColor orangeColor]];
225+
config.finishFonts = @[[UIFont boldSystemFontOfSize:20],[UIFont systemFontOfSize:20]];
226+
config.finishTextColors = @[[UIColor greenColor],[UIColor orangeColor]];
227+
208228
[self.view addSubview:({
209229

210230
UILabel *label = [[UILabel alloc] init];
@@ -215,8 +235,12 @@ - (void)jhSetupViews
215235
JHVerificationCodeView *codeView =
216236
[[JHVerificationCodeView alloc] initWithFrame:CGRectMake(10, 420, kScreenWidth-20, 30)
217237
config:config];
218-
codeView.finishBlock = ^(NSString *code) {
238+
codeView.finishBlock = ^(JHVerificationCodeView *codeView, NSString *code) {
219239
label.text = code;
240+
241+
// 根据最后输入结果,判断显示哪种颜色
242+
NSUInteger index = [code isEqualToString:@"Asdf"] ? 1 : 0;
243+
[codeView showInputFinishColorWithIndex:index];
220244
};
221245
codeView.inputBlock = ^(NSString *code) {
222246
NSLog(@"example 4 code:%@",code);

0 commit comments

Comments
 (0)