本文实例为大家分享了iOS生成图片数字字母验证的具体代码,供大家参考,具体内容如下
直接上代码,注释很详细
#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 kRandomcolor [UIcolor graycolor];#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; // [UIcolor graycolor] //显示一个随机验证码 [self changeCaptcha]; } return self;}#pragma mark 更换验证码,得到更换的验证码的字符串-(voID)changeCaptcha{ //<一>从字符数组中随机抽取相应数量的字符,组成验证码字符串 //数组中存放的是全部可选的字符,可以是字母,也可以是中文 self.changeArray = [[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]; //如果能确定最大需要的容量,使用initWithCapacity:来设置,好处是当元素个数不超过容量时,添加元素不需要重新分配内存 NSMutableString *getStr = [[NSMutableString alloc] initWithCapacity:kCharCount]; self.changeString = [[NSMutableString alloc] initWithCapacity:kCharCount]; //随机从数组中选取需要个数的字符,然后拼接为一个字符串 for(int i = 0; i < kCharCount; i++) { NSInteger index = arc4random() % ([self.changeArray count] - 1); getStr = [self.changeArray objectAtIndex:index]; self.changeString = (NSMutableString *)[self.changeString stringByAppendingString:getStr]; }}#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:20.0]}]; 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,pY); //画线 CGContextstrokePath(context); }}@end
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。
总结以上是内存溢出为你收集整理的iOS生成图片数字字母验证效果全部内容,希望文章能够帮你解决iOS生成图片数字字母验证效果所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)