iOS生成本地随机验证码

原文链接:http://www.cnblogs.com/jerehedu/p/4527707.html

效果图:

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAALgAAAA/CAYAAABThgKnAAAL8mlDQ1BJQ0MgUHJvZmlsZQAASImVVwdUk0nXnrekAQklgICU0DuG3qSE3kE62AhJIIEQYgIo2JXFFVwLIqKorOgi2NcCyFoRxcKq2LAviCgo62IBCyrfJKDuf87v+c53c27meZ/33jt37jszZwYAWjJbLBaiKgDkiPIkscF+jOSUVAbpL4ADRYABB2DF5kjFrJiYCPBDGbkNEFl7w1YW68d2/6+ocnlSDgBIDMTpXCknB+IjAGDeHLEkDwD8LeSN5+aJISZoQawugQlCbCPDmePYV4bTx3G83CY+1h9iPgBkJTZbkgkAFfoDRgEnE7bUCoiZIq5ABHEbxN4cPpsLAA2H2CYnJ1eGmRBbpP8rTub/iZn+LSabnfkNj49FLuQAgVQsZBf+j+X475IjzP/ahxFUJb4kJFbWwrrVZ+eGyzBRVkNRelT0BN8i4MrtFSA+z88PSYAY1hK5xpH6w5oBGsT9XHZA+ITNSH52AmucRwFbIvdVh5gmyAuN/2ovyY2diI8a8aSBcRDDWYTa8XmhEeM5oN4iYZQMW0AckCEICp2wFxbx45MmbOYVCBKjJnwXSbPjwif6XV3E94+asNkiyY9NmOBrMyRBsjHC74MezJHKc5Px3Rz29xzy+PEhE/glT5oc8ZXn8gICv+bPEyXETfh+Eef5xX7lxUL5XB+3EQbHjtcEU5IWxMl81SDWzIMTbIK3yGKHxYyPC3MQ58V8qw9IBWwgBUKQC0Qt9YcudDJBHMgGWYAHJCAHRMC3hVAlIBIymXJeCFUE/gZ5YAB6Rso8gQA+5UIrme9TmSeujrvhLNwH98SdcXeiE9EcqiVgwDaA6E60J7oAFsgAAlQbRs8CMfBfBGOwYXQhYBMeEXoI90A05NJhb9nyHrmAIX+SZcCFPXMAfyLLKPAEZpkHfyNVuYUeiXwQAgT6Z6EPA3J8/csgAGYoBWJ57lkZ3jIbXBf3xr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+d9PGPlroEZrr5454gpBNGN3Q3a1IWNzQVYXJ63d9OOfvM/b2XL1uFqWO0+iH/8hkBcMgI1hgWFImjJ1O2cwfZ/9xlTGwZgySqoGOP9vlZt3LhhVnxW5b71yxNkDbz7/65rqzMYhE8jZGPKGUTACOxkBGUPBaHKGwfuKRD97YTLPz5gySqoGOP/joNUOcM4RPzjUpuaIc55KI4RBKjG1oQWladWNqFLDnsmYMkqqnqLzv5rgfxy0miWfI94ZEZO7RvcjY8mof1/CbaP3EZy7Vr2s9n8jWK8fya9ec+KKpv83gqKLhAZWiwaqpiirpWGinkIDrAEBcIEDU2tAANzU3SsaJwAuMGBqDQiAm7p7ReMEwAUGTK0BAXBTd69onAC4wICpNSAAburuFY0TABcYMLUGBMBN3b2icf8HNHnAoJwsJMcAAAAASUVORK5CYII=" alt="" />

CaptchaView.h

@interface CaptchaView : UIView

@property (nonatomic, retain) NSArray *changeArray; //字符素材数组
@property (nonatomic, retain) NSMutableString *changeString; //验证码的字符串

CaptchaView.m

#import "CaptchaView.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 CaptchaView
@synthesize changeString,changeArray; - (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) { self.layer.cornerRadius = 5.0; //设置layer圆角半径
self.layer.masksToBounds = YES; //隐藏边界
self.backgroundColor = kRandomColor; //显示一个随机验证码
[self changeCaptcha];
} return self;
}
#pragma mark 更换验证码,得到更换的验证码的字符串
-(void)changeCaptcha
{
//<一>从字符数组中随机抽取相应数量的字符,组成验证码字符串
//数组中存放的是全部可选的字符,可以是字母,也可以是中文
self.changeArray = [[NSArray alloc] initWithObjects:@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"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]; //如果能确定最大需要的容量,使用initWithCapacity:来设置,好处是当元素个数不超过容量时,添加元素不需要重新分配内存
NSMutableString *getStr = [[NSMutableString alloc] initWithCapacity:kCharCount];
self.changeString = [[NSMutableString alloc] initWithCapacity:kCharCount]; //随机从数组中选取需要个数的字符,然后拼接为一个字符串
for(int i = ; i < kCharCount; i++)
{
NSInteger index = arc4random() % ([self.changeArray count] - );
getStr = [self.changeArray objectAtIndex:index]; self.changeString = (NSMutableString *)[self.changeString stringByAppendingString:getStr];
} //<2>从网络获取字符串,然后把得到的字符串在本地绘制出来(网络获取步骤在这省略) } #pragma mark 点击view时调用,因为当前类自身就是UIView,点击更换验证码可以直接写到这个方法中,不用再额外添加手势
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//点击界面,切换验证码
[self changeCaptcha]; //setNeedsDisplay调用drawRect方法来实现view的绘制
[self setNeedsDisplay];
} #pragma mark 绘制界面(1.UIView初始化后自动调用; 2.调用setNeedsDisplay方法时会自动调用)
- (void)drawRect:(CGRect)rect {
// 重写父类方法,首先要调用父类的方法
[super drawRect:rect]; //设置随机背景颜色
self.backgroundColor = kRandomColor; //获得要显示验证码字符串,根据长度,计算每个字符显示的大概位置
NSString *text = [NSString stringWithFormat:@"%@",self.changeString];
CGSize cSize = [@"S" sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:]}];
int width = rect.size.width / text.length - cSize.width;
int height = rect.size.height - cSize.height;
CGPoint point; //依次绘制每一个字符,可以设置显示的每个字符的字体大小、颜色、样式等
float pX, pY;
for (int i = ; 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 = ; 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
 
上一篇:记住用户名


下一篇:定时器