@@ -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
0 commit comments