月圆之夜,紫禁之巅,一剑西来,天外飞仙。

0%

iOS 随机验证码

最近在项目中用到了随机验证码,在这里简单做个记录。
效果图:

20181004164927131

主要代码:

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
.h文件
#import <UIKit/UIKit.h>

typedef NS_ENUM(NSInteger,XBGetVerificationCodeType)

{

XBGetVerificationCodeOne = 0,//只数字

XBGetVerificationCodeTwo,//只字母

XBGetVerificationCodeThree,//数字字母混合
};

typedef void(^setShowCodeNumber)(NSString * codeNumber);

@interface XBGetVerificationCodeView : UIView

@property(nonatomic,assign)NSInteger verificationNumber;//验证码位数

@property(nonatomic,assign)XBGetVerificationCodeType tmpCodeType;

@property(nonatomic,strong)setShowCodeNumber setShowCodeNumber;//获取到的验证码

-(void)setCodeTitleColor:(UIColor *)color;// 设置验证码字体颜色

-(void)setCodeTitleFont:(UIFont *)font;//设置验证码字体大小

@end
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
.m 文件
#import "XBGetVerificationCodeView.h"

@interface XBGetVerificationCodeView()

@property(nonatomic,strong)UIButton * btn;

@end

@implementation XBGetVerificationCodeView

-(id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];

if(self)
{
self.verificationNumber = 6;

self.btn = [UIButton buttonWithType:UIButtonTypeCustom];

[self.btn setFrame:CGRectMake(0, 0, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame))];

[self.btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

[self.btn setTitle:@"获取验证码" forState:UIControlStateNormal];

[self.btn.titleLabel setFont:[UIFont systemFontOfSize:14.0]];

[self.btn addTarget:self action:@selector(setBtnAction:) forControlEvents:UIControlEventTouchUpInside];

[self addSubview:self.btn];
}

return self;
}

-(void)setBtnAction:(UIButton *)sender
{
if(sender && [sender isKindOfClass:[UIButton class]])
{
[self.btn setTitle:[NSString stringWithFormat:@"%@",[self getRandomStringWithNum:self.verificationNumber]] forState:UIControlStateNormal];

if(self.setShowCodeNumber)
{
self.setShowCodeNumber([NSString stringWithFormat:@"%@",[self getRandomStringWithNum:self.verificationNumber]]);
}

}
}

-(void)setCodeTitleColor:(UIColor *)color
{
[self.btn setTitleColor:color forState:UIControlStateNormal];
}
-(void)setCodeTitleFont:(UIFont *)font
{
[self.btn.titleLabel setFont:font];
}
-(NSString *)getRandomStringWithNum:(NSInteger)num
{

NSString *string = [[NSString alloc]init];

if(self.tmpCodeType == XBGetVerificationCodeOne)
{
for (int i = 0; i < num; i++) {

int figure = arc4random() % 10;

NSString *tempString = [NSString stringWithFormat:@"%d", figure];

string = [string stringByAppendingString:tempString];

}
return string;
}
else if(self.tmpCodeType == XBGetVerificationCodeTwo)
{
for (int i = 0; i < num; i++) {

int figure = (arc4random() % 26) + 97;

char character = figure;

NSString *tempString = [NSString stringWithFormat:@"%c", character];

string = [string stringByAppendingString:tempString];

}
return string;
}
else
{
for (int i = 0; i < num; i++) {
int number = arc4random() % 36;
if (number < 10) {
int figure = arc4random() % 10;
NSString *tempString = [NSString stringWithFormat:@"%d", figure];
string = [string stringByAppendingString:tempString];
}else {
int figure = (arc4random() % 26) + 97;
char character = figure;
NSString *tempString = [NSString stringWithFormat:@"%c", character];
string = [string stringByAppendingString:tempString];
}
}
return string;
}


}
@end

使用

1
2
3
4
5
6
7
8
9
10
11
12
XBGetVerificationCodeView * xbView = [[XBGetVerificationCodeView alloc]initWithFrame:CGRectMake(100, 100, 100, 40)];

[xbView setBackgroundColor:[UIColor orangeColor]];

xbView.tmpCodeType = XBGetVerificationCodeThree;

[xbView setSetShowCodeNumber:^(NSString *codeNumber) {

NSLog(@"验证码为:%@",codeNumber);
}];

[self.view addSubview:xbView];

demo地址:https://gitee.com/xiaobaidxg/VerificationCode.git