随机获取验证码

作者: 小菜鸟爱开发 | 来源:发表于2016-04-11 11:18 被阅读339次

简单封装了一个随机获取验证码;

#import@interface AuthcodeView : UIView

@property (strong, nonatomic) NSArray *dataArray;//字符素材数组

@property (strong, nonatomic) NSMutableString *authCodeStr;//验证码字符串

- (void)getAuthcode;

@end

#import "AuthcodeView.h"

#define kRandomColor  [UIColor colorWithRed:arc4random() % 256 / 256.0 green:arc4random() % 256 / 256.0 blue:arc4random() % 256 / 256.0 alpha:1.0];

#define kLineCount 6

#define kLineWidth 1.0

#define kCharCount 4

#define kFontSize [UIFont systemFontOfSize:arc4random() % 5 + 15]

@implementation AuthcodeView

- (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

{

//字符串素材

self.layer.cornerRadius = 3.0f;

self.layer.masksToBounds = YES;

self.backgroundColor = kRandomColor;

_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];

}

- (void)drawRect:(CGRect)rect

{

[super drawRect:rect];

//设置随机背景颜色

self.backgroundColor = kRandomColor;

//根据要显示的验证码字符串,根据长度,计算每个字符串显示的位置

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];

[textC drawAtPoint:point withAttributes:@{NSFontAttributeName:kFontSize}];

}

//调用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);

}

}

@end

相关文章

  • 2018.8.13

    注册随机验证码 获取验证码提交 var inp = document.getElementById('input'...

  • iOS-仿智联字符图片验证码

    项目中有时候会有这种需求: 获取这种 随机字符组成的图片验证码.随机字符组成的图片验证码, 字符位数可改变, 字符...

  • 随机获取验证码

    简单封装了一个随机获取验证码; #import@interface AuthcodeView : UIView@p...

  • 随机验证码

    记录一个随机验证码 获取验证码时使用 这是我看到别人写的修改的。 用法: 确定逻辑判断 完!

  • python小程序集合

    获取随机唯一验证码 关键:随机且唯一,用数字和字母组合,参考网上的写法,就是用数字加分隔符加随机字符串的形式,这样...

  • 登陆判断

    问题:从26个大小写字母和10个数字中随机获取4位验证码,生成验证码和判断前端:html代码: 登...

  • iOS倒计时

    pragma mark ---获取验证码 /**获取验证码,倒计时@param sender 获取验证码的按...

  • SpringBoot通过redis缓存完成邮箱验证登录。

    1.实现流程 获取用户输入的邮箱地址,发送随机成功的验证码。把验证码和邮箱地址存入redis缓存,表单提交时判断缓...

  • JS实现数字字母混合验证码(数字+大写字母+小写字母)

    ?作者主页? 我之前写了一篇《JS实现随机验证码》,可以产生随机数字、随机颜色、随机位置,但只能产生数字验证码,今...

  • 三种方法使用jquery获取验证码倒计时

    三种方法使用jquery获取验证码倒计时,并设置防重复点击 获取验证码1 获取验证码2 获取验证码3 /...

网友评论

    本文标题:随机获取验证码

    本文链接:https://www.haomeiwen.com/subject/cdkilttx.html