在iOS项目中登录注册经常会用到随机验证码,尤其是以图片形式生成的验证码,可以减少使用第三方资源的代码使用,只在本地生成并验证即可,本文即介绍生成随机图片验证码的流程,验证码包括阿拉伯数字0-9,英文大写字母A-Z,英文小写字母a-z,生成的验证码区分大小写。若想改成不区分大小写,只需要在取码的时候添加判断即可。
一、首先创建一个继承自UIView的类,在.h文件中添加外界需要的属性和方法:
@property (strong, nonatomic) NSMutableString *authCodeStr;//验证码字符串 - (void)getAuthcode;
二、在.m文件中添加宏定义:
//背景随机颜色 #define kRandomColorAL [UIColor colorWithRed:arc4random() % 256 / 256.0 green:arc4random() % 256 / 256.0 blue:arc4random() % 256 / 256.0 alpha:0.5]; //干扰线随机颜色 #define kRandomColor [UIColor colorWithRed:arc4random() % 256 / 256.0 green:arc4random() % 256 / 256.0 blue:arc4random() % 256 / 256.0 alpha:1]; //干扰线数量 #define kLineCount 4 //干扰线宽度 #define kLineWidth 1.0 //验证码数量 #define kCharCount 4 //验证码大小 #define kFontSize (arc4random() % 5 + 20)
三、初始化:
- (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { self.layer.cornerRadius = 5.0f; self.layer.masksToBounds = YES; self.backgroundColor = kRandomColor; [self getAuthcode];//获得随机验证码 } return self; }
四、获取随机验证码方法:
#pragma mark 获得随机验证码 - (void)getAuthcode { //字符串素材 _dataArray = [[NSArray alloc] initWithObjects:@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J",@"K",@"L",@"M",@"N",@"O",@"P",@"Q",@"R",@"S",@"T",@"U",@"V",@"W",@"X",@"Y",@"Z",@"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",@"i",@"j",@"k",@"l",@"m",@"n",@"o",@"p",@"q",@"r",@"s",@"t",@"u",@"v",@"w",@"x",@"y",@"z",nil]; _authCodeStr = [[NSMutableString alloc] initWithCapacity:kCharCount]; //随机从数组中选取需要个数的字符串,拼接为验证码字符串 for (int i = 0; i < kCharCount; i++) { NSInteger index = arc4random() % (_dataArray.count-1); NSString *tempStr = [_dataArray objectAtIndex:index]; _authCodeStr = (NSMutableString *)[_authCodeStr stringByAppendingString:tempStr]; } }
五、点击界面切换验证码:
#pragma mark 点击界面切换验证码 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [self getAuthcode]; //setNeedsDisplay调用drawRect方法来实现view的绘制 [self setNeedsDisplay]; }
六、绘制:
#pragma mark 绘制 - (void)drawRect:(CGRect)rect { [super drawRect:rect]; //设置随机背景颜色 self.backgroundColor = kRandomColorAL; //根据要显示的验证码字符串,根据长度,计算每个字符串显示的位置 NSString *text = [NSString stringWithFormat:@"%@",_authCodeStr]; CGSize cSize = [@"A" sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:20]}]; int width = rect.size.width/text.length - cSize.width; int height = rect.size.height - cSize.height; CGPoint point; //依次绘制每一个字符,可以设置显示的每个字符的字体大小、颜色、样式等 float pX,pY; for ( int i = 0; i<text.length; i++) { pX = arc4random() % width + rect.size.width/text.length * i; pY = arc4random() % height; point = CGPointMake(pX, pY); unichar c = [text characterAtIndex:i]; NSString *textC = [NSString stringWithFormat:@"%C", c]; UIFontDescriptor *attributeFontDescriptor = [UIFontDescriptor fontDescriptorWithFontAttributes: @{UIFontDescriptorFamilyAttribute: @"Marion", UIFontDescriptorNameAttribute:@"HanziPenSC-W3", UIFontDescriptorSizeAttribute: @40.0, UIFontDescriptorMatrixAttribute:[NSValue valueWithCGAffineTransform:CGAffineTransformMakeRotation(M_1_PI*(arc4random() % 2 )) ]}]; UIFont * font = [UIFont fontWithDescriptor:attributeFontDescriptor size:kFontSize]; UIColor *color = kRandomColor; [textC drawAtPoint:point withAttributes:@{NSFontAttributeName:font,NSForegroundColorAttributeName:color}]; } //调用drawRect:之前,系统会向栈中压入一个CGContextRef,调用UIGraphicsGetCurrentContext()会取栈顶的CGContextRef CGContextRef context = UIGraphicsGetCurrentContext(); //设置线条宽度 CGContextSetLineWidth(context, kLineWidth); //绘制干扰线 for (int i = 0; i < kLineCount; i++) { UIColor *color = kRandomColor; CGContextSetStrokeColorWithColor(context, color.CGColor);//设置线条填充色 //设置线的起点 pX = arc4random() % (int)rect.size.width; pY = arc4random() % (int)rect.size.height; CGContextMoveToPoint(context, pX, pY); //设置线终点 pX = arc4random() % (int)rect.size.width; pY = arc4random() % (int)rect.size.height; CGContextAddLineToPoint(context, pX, pY); //画线 CGContextStrokePath(context); } }
七、调用,在需要使用验证码的类中调用:
1、定义属性:
@property (nonatomic, strong) MaxAuthCodeView * authcodeImage;/**< 验证码视图*/
2、初始化:
self.authcodeImage = [[MaxAuthCodeView alloc] initWithFrame:CGRectMake(50, 100, 120, 60)]; [self.view addSubview:self.authcodeImage]; [self.authcodeImage getAuthcode];
3、获取验证码:
NSLog(@"%@",self.authcodeImage.authCodeStr);
八、希望各位能够获取到有用的信息,谢谢!
demo下载地址:https://github.com/MaxLi7681/AuthCode