Skip to content

Commit

Permalink
add Finish Status
Browse files Browse the repository at this point in the history
  • Loading branch information
xjh093 committed Dec 24, 2019
1 parent ac217e5 commit deda7a8
Show file tree
Hide file tree
Showing 8 changed files with 157 additions and 51 deletions.
Binary file removed .DS_Store
Binary file not shown.
2 changes: 1 addition & 1 deletion JHVerificationCodeView.podspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Pod::Spec.new do |s|
s.name = "JHVerificationCodeView"
s.version = "1.3.5"
s.version = "1.3.6"
s.summary = "A simple Verification Code View."
s.homepage = "https://github.com/xjh093/JHVerificationCodeView"
s.license = "MIT"
Expand Down
1 change: 1 addition & 0 deletions JHVerificationCodeView.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@
developmentRegion = English;
hasScannedForEncodings = 0;
knownRegions = (
English,
en,
Base,
);
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Bucket
uuid = "5E172D97-7924-44AA-85D1-5D1DE43E5BDC"
type = "1"
version = "2.0">
</Bucket>
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

// version: 1.3.6
// date: 2019-12-24

#import <UIKit/UIKit.h>

typedef NS_ENUM(NSUInteger, JHVCConfigInputType) {
Expand All @@ -36,6 +39,9 @@ typedef NS_ENUM(NSUInteger, JHVCConfigInputType) {
};

@interface JHVCConfig : NSObject

//============================ Initialization ============================

///输入框个数
@property (assign, nonatomic) NSInteger inputBoxNumber;
///单个输入框的宽度
Expand All @@ -52,15 +58,13 @@ typedef NS_ENUM(NSUInteger, JHVCConfigInputType) {
@property (assign, nonatomic) CGFloat leftMargin;
///单个输入框的颜色, Default is lightGrayColor
@property (strong, nonatomic) UIColor *inputBoxColor;
///单个输入框输入时的颜色
@property (strong, nonatomic) UIColor *inputBoxHighlightedColor;
///光标颜色, Default is blueColor
@property (strong, nonatomic) UIColor *tintColor;
///显示 或 隐藏
@property (assign, nonatomic) BOOL secureTextEntry;
///字体
///字体, Default is [UIFont boldSystemFontOfSize:16]
@property (strong, nonatomic) UIFont *font;
///颜色
///颜色, Default is [UIColor blackColor]
@property (strong, nonatomic) UIColor *textColor;
///输入类型:数字+字母,数字,字母. Default is 'JHVCConfigInputType_Number_Alphabet'
@property (nonatomic, assign) JHVCConfigInputType inputType;
Expand All @@ -74,20 +78,38 @@ typedef NS_ENUM(NSUInteger, JHVCConfigInputType) {
@property (nonatomic, assign) CGSize underLineSize;
///下划线颜色, Default is lightGrayColor
@property (nonatomic, strong) UIColor *underLineColor;
///下划线高亮颜色
@property (nonatomic, strong) UIColor *underLineHighlightedColor;
///自定义的输入占位字符,secureTextEntry = NO,有效
@property (nonatomic, copy) NSString *customInputHolder;
///设置键盘类型
@property (nonatomic, assign) UIKeyboardType keyboardType;
///使用系统的密码键盘
@property (nonatomic, assign) BOOL useSystemPasswordKeyboard;

//============================ Input ============================

///单个输入框输入时的颜色
@property (strong, nonatomic) UIColor *inputBoxHighlightedColor;
///下划线高亮颜色
@property (nonatomic, strong) UIColor *underLineHighlightedColor;

//============================ Finish ============================
/* 输入完成后,可能根据不同的状态,显示不同的颜色。 */

///单个输入框输入时的颜色
@property (strong, nonatomic) NSArray<UIColor*> *inputBoxFinishColors;
///下划线高亮颜色
@property (nonatomic, strong) NSArray<UIColor*> *underLineFinishColors;
///输入完成时字体
@property (strong, nonatomic) NSArray<UIFont*> *finishFonts;
///输入完成时颜色
@property (strong, nonatomic) NSArray<UIColor*> *finishTextColors;

@end

@interface JHVerificationCodeView : UIView

@property (copy, nonatomic) void (^inputBlock)(NSString *code);
@property (copy, nonatomic) void (^finishBlock)(NSString *code);
@property (copy, nonatomic) void (^finishBlock)(JHVerificationCodeView *codeView, NSString *code);

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

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

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

@end
127 changes: 89 additions & 38 deletions JHVerificationCodeView/JHVerificationCodeView/JHVerificationCodeView.m
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ - (instancetype)init{
_inputBoxSpacing = 5;
_inputBoxColor = [UIColor lightGrayColor];
_tintColor = [UIColor blueColor];
_font = [UIFont boldSystemFontOfSize:16];
_textColor = [UIColor blackColor];
_showFlickerAnimation = YES;
_underLineColor = [UIColor lightGrayColor];
}
Expand All @@ -50,6 +52,8 @@ - (instancetype)init{
@interface JHVerificationCodeView()
@property (strong, nonatomic) JHVCConfig *config;
@property (strong, nonatomic) UITextView *textView;
@property (nonatomic, assign) BOOL inputFinish;
@property (nonatomic, assign) NSUInteger inputFinishIndex;
@end

@implementation JHVerificationCodeView
Expand Down Expand Up @@ -226,26 +230,8 @@ - (void)xx_textChange:(NSNotification *)noti
return;
}

NSInteger count = _config.inputBoxNumber;

// set default
for (int i = 0; i < count; ++i) {
UITextField *textField = self.subviews[i];
textField.text = @"";

if (_config.inputBoxColor) {
textField.layer.borderColor = _config.inputBoxColor.CGColor;
}
if (_config.showFlickerAnimation) {
CALayer *layer = textField.layer.sublayers[0];
layer.hidden = YES;
[layer removeAnimationForKey:kFlickerAnimation];
}
if (_config.showUnderLine) {
UIView *underLine = [textField viewWithTag:100];
underLine.backgroundColor = _config.underLineColor;
}
}
[self xx_setDefault];

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

text = mstr;
NSInteger count = _config.inputBoxNumber;
if (text.length > count) {
text = [text substringToIndex:count];
}
Expand All @@ -281,44 +268,99 @@ - (void)xx_textChange:(NSNotification *)noti
_inputBlock(text);
}

//NSLog(@"%@",text);

// set value
for (int i = 0; i < text.length; ++i) {
unichar c = [text characterAtIndex:i];
[self xx_setValue:text];

// Flicker Animation
[self xx_flickerAnimation:text];

if (_inputFinish) {
[self xx_finish];
}
}

- (void)xx_setDefault
{
for (int i = 0; i < _config.inputBoxNumber; ++i) {
UITextField *textField = self.subviews[i];
textField.text = [NSString stringWithFormat:@"%c",c];
if (!textField.secureTextEntry && _config.customInputHolder.length > 0) {
textField.text = _config.customInputHolder;
}
textField.text = @"";

if (_config.inputBoxHighlightedColor) {
textField.layer.borderColor = _config.inputBoxHighlightedColor.CGColor;
if (_config.inputBoxColor) {
textField.layer.borderColor = _config.inputBoxColor.CGColor;
}
if (_config.showFlickerAnimation) {
CALayer *layer = textField.layer.sublayers[0];
layer.hidden = YES;
[layer removeAnimationForKey:kFlickerAnimation];
}

if (_config.showUnderLine && _config.underLineHighlightedColor) {
if (_config.showUnderLine) {
UIView *underLine = [textField viewWithTag:100];
underLine.backgroundColor = _config.underLineHighlightedColor;
underLine.backgroundColor = _config.underLineColor;
}
}

// Flicker Animation
}

- (void)xx_flickerAnimation:(NSString *)text
{
if (_config.showFlickerAnimation && text.length < self.subviews.count) {
UITextField *textField = self.subviews[text.length];
CALayer *layer = textField.layer.sublayers[0];
layer.hidden = NO;
[layer addAnimation:[self xx_alphaAnimation] forKey:kFlickerAnimation];
}
}

- (void)xx_setValue:(NSString *)text
{
_inputFinish = (text.length == _config.inputBoxNumber);

if (text.length == count) {
[self xx_finish];
for (int i = 0; i < text.length; ++i) {
unichar c = [text characterAtIndex:i];
UITextField *textField = self.subviews[i];
textField.text = [NSString stringWithFormat:@"%c",c];
if (!textField.secureTextEntry && _config.customInputHolder.length > 0) {
textField.text = _config.customInputHolder;
}

// Input Status
UIFont *font = _config.font;
UIColor *color = _config.textColor;
UIColor *inputBoxColor = _config.inputBoxHighlightedColor;
UIColor *underLineColor = _config.underLineHighlightedColor;

// Finish Status
if (_inputFinish) {
if (_inputFinishIndex < _config.finishFonts.count) {
font = _config.finishFonts[_inputFinishIndex];
}
if (_inputFinishIndex < _config.finishTextColors.count) {
color = _config.finishTextColors[_inputFinishIndex];
}
if (_inputFinishIndex < _config.inputBoxFinishColors.count) {
inputBoxColor = _config.inputBoxFinishColors[_inputFinishIndex];
}
if (_inputFinishIndex < _config.underLineFinishColors.count) {
underLineColor = _config.underLineFinishColors[_inputFinishIndex];
}
}

textField.font = font;
textField.textColor = color;

if (inputBoxColor) {
textField.layer.borderColor = inputBoxColor.CGColor;
}
if (_config.showUnderLine && underLineColor) {
UIView *underLine = [textField viewWithTag:100];
underLine.backgroundColor = underLineColor;
}
}
}

- (void)xx_finish
{
if (_finishBlock) {
_finishBlock(_textView.text);
_finishBlock(self, _textView.text);
}

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
Expand All @@ -331,7 +373,16 @@ - (void)xx_finish
- (void)clear
{
_textView.text = @"";
[[NSNotificationCenter defaultCenter] postNotificationName:UITextViewTextDidChangeNotification object:_textView];

[self xx_setDefault];
[self xx_flickerAnimation:_textView.text];
}

- (void)showInputFinishColorWithIndex:(NSUInteger)index
{
_inputFinishIndex = index;

[self xx_setValue:_textView.text];
}

@end
34 changes: 29 additions & 5 deletions JHVerificationCodeView/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,15 @@ - (void)jhSetupViews
config.inputBoxBorderWidth = 1;
config.inputBoxCornerRadius = 5;

config.customInputHolder = @"🔒";
//config.customInputHolder = @"🔒";

config.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
config.useSystemPasswordKeyboard = YES;

config.inputBoxFinishColors = @[[UIColor redColor],[UIColor orangeColor]];
config.finishFonts = @[[UIFont boldSystemFontOfSize:20],[UIFont systemFontOfSize:20]];
config.finishTextColors = @[[UIColor greenColor],[UIColor orangeColor]];

[self.view addSubview:({

UILabel *label = [[UILabel alloc] init];
Expand All @@ -77,8 +81,12 @@ - (void)jhSetupViews
JHVerificationCodeView *codeView =
[[JHVerificationCodeView alloc] initWithFrame:CGRectMake(10, 150, kScreenWidth-20, 30)
config:config];
codeView.finishBlock = ^(NSString *code) {
codeView.finishBlock = ^(JHVerificationCodeView *codeView, NSString *code) {
label.text = code;

// 根据最后输入结果,判断显示哪种颜色
NSUInteger index = [code isEqualToString:@"123asd"] ? 1 : 0;
[codeView showInputFinishColorWithIndex:index];
};
codeView.inputBlock = ^(NSString *code) {
NSLog(@"example 1 code:%@",code);
Expand Down Expand Up @@ -113,6 +121,10 @@ - (void)jhSetupViews

config.customInputHolder = @"🈲";

config.inputBoxFinishColors = @[[UIColor orangeColor],[UIColor redColor]];
config.finishFonts = @[[UIFont boldSystemFontOfSize:20],[UIFont systemFontOfSize:20]];
config.finishTextColors = @[[UIColor orangeColor],[UIColor greenColor]];

[self.view addSubview:({

UILabel *label = [[UILabel alloc] init];
Expand All @@ -123,8 +135,12 @@ - (void)jhSetupViews
JHVerificationCodeView *codeView =
[[JHVerificationCodeView alloc] initWithFrame:CGRectMake(10, 240, kScreenWidth-20, 30)
config:config];
codeView.finishBlock = ^(NSString *code) {
codeView.finishBlock = ^(JHVerificationCodeView *codeView, NSString *code) {
label.text = code;

// 根据最后输入结果,判断显示哪种颜色
NSUInteger index = [code isEqualToString:@"123456"] ? 1 : 0;
[codeView showInputFinishColorWithIndex:index];
};
codeView.inputBlock = ^(NSString *code) {
NSLog(@"example 2 code:%@",code);
Expand Down Expand Up @@ -168,7 +184,7 @@ - (void)jhSetupViews
JHVerificationCodeView *codeView =
[[JHVerificationCodeView alloc] initWithFrame:CGRectMake(10, 330, kScreenWidth-20, 30)
config:config];
codeView.finishBlock = ^(NSString *code) {
codeView.finishBlock = ^(JHVerificationCodeView *codeView, NSString *code) {
label.text = code;
};
codeView.inputBlock = ^(NSString *code) {
Expand Down Expand Up @@ -205,6 +221,10 @@ - (void)jhSetupViews
config.underLineColor = [UIColor brownColor];
config.underLineHighlightedColor = [UIColor redColor];

config.underLineFinishColors = @[[UIColor blueColor],[UIColor orangeColor]];
config.finishFonts = @[[UIFont boldSystemFontOfSize:20],[UIFont systemFontOfSize:20]];
config.finishTextColors = @[[UIColor greenColor],[UIColor orangeColor]];

[self.view addSubview:({

UILabel *label = [[UILabel alloc] init];
Expand All @@ -215,8 +235,12 @@ - (void)jhSetupViews
JHVerificationCodeView *codeView =
[[JHVerificationCodeView alloc] initWithFrame:CGRectMake(10, 420, kScreenWidth-20, 30)
config:config];
codeView.finishBlock = ^(NSString *code) {
codeView.finishBlock = ^(JHVerificationCodeView *codeView, NSString *code) {
label.text = code;

// 根据最后输入结果,判断显示哪种颜色
NSUInteger index = [code isEqualToString:@"Asdf"] ? 1 : 0;
[codeView showInputFinishColorWithIndex:index];
};
codeView.inputBlock = ^(NSString *code) {
NSLog(@"example 4 code:%@",code);
Expand Down

0 comments on commit deda7a8

Please sign in to comment.