最近在项目中用到了随机验证码,在这里简单做个记录。
效果图:
主要代码:
.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
.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
使用
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];