iOS生成图片数字字母验证效果
本文实例为大家分享了iOS生成图片数字字母验证的具体代码,供大家参考,具体内容如下
直接上代码,注释很详细
#import"CaptchaView.h"
#definekRandomColor[UIColorcolorWithRed:arc4random()%256/256.0green:arc4random()%256/256.0blue:arc4random()%256/256.0alpha:1.0];
//#definekRandomColor[UIColorgrayColor];
#definekLineCount6
#definekLineWidth1.0
#definekCharCount4
#definekFontSize[UIFontsystemFontOfSize:arc4random()%5+15]
@implementationCaptchaView
@synthesizechangeString,changeArray;
-(instancetype)initWithFrame:(CGRect)frame
{
if(self=[superinitWithFrame:frame]){
self.layer.cornerRadius=5.0;//设置layer圆角半径
self.layer.masksToBounds=YES;//隐藏边界
self.backgroundColor=kRandomColor;
//[UIColorgrayColor]
//显示一个随机验证码
[selfchangeCaptcha];
}
returnself;
}
#pragmamark更换验证码,得到更换的验证码的字符串
-(void)changeCaptcha
{
//<一>从字符数组中随机抽取相应数量的字符,组成验证码字符串
//数组中存放的是全部可选的字符,可以是字母,也可以是中文
self.changeArray=[[NSArrayalloc]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=[[NSMutableStringalloc]initWithCapacity:kCharCount];
self.changeString=[[NSMutableStringalloc]initWithCapacity:kCharCount];
//随机从数组中选取需要个数的字符,然后拼接为一个字符串
for(inti=0;i
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。