這篇文章給大家介紹怎么在iOS中實(shí)現(xiàn)生成圖片數(shù)字字母驗(yàn)證效果,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。
我們提供的服務(wù)有:成都網(wǎng)站建設(shè)、成都做網(wǎng)站、微信公眾號(hào)開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、原平ssl等。為上1000家企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的原平網(wǎng)站制作公司
#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; //設(shè)置layer圓角半徑 self.layer.masksToBounds = YES; //隱藏邊界 self.backgroundColor = kRandomColor; // [UIColor grayColor] //顯示一個(gè)隨機(jī)驗(yàn)證碼 [self changeCaptcha]; } return self; } #pragma mark 更換驗(yàn)證碼,得到更換的驗(yàn)證碼的字符串 -(void)changeCaptcha { //<一>從字符數(shù)組中隨機(jī)抽取相應(yīng)數(shù)量的字符,組成驗(yàn)證碼字符串 //數(shù)組中存放的是全部可選的字符,可以是字母,也可以是中文 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:來設(shè)置,好處是當(dāng)元素個(gè)數(shù)不超過容量時(shí),添加元素不需要重新分配內(nèi)存 NSMutableString *getStr = [[NSMutableString alloc] initWithCapacity:kCharCount]; self.changeString = [[NSMutableString alloc] initWithCapacity:kCharCount]; //隨機(jī)從數(shù)組中選取需要個(gè)數(shù)的字符,然后拼接為一個(gè)字符串 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 點(diǎn)擊view時(shí)調(diào)用,因?yàn)楫?dāng)前類自身就是UIView,點(diǎn)擊更換驗(yàn)證碼可以直接寫到這個(gè)方法中,不用再額外添加手勢(shì) -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { //點(diǎn)擊界面,切換驗(yàn)證碼 [self changeCaptcha]; //setNeedsDisplay調(diào)用drawRect方法來實(shí)現(xiàn)view的繪制 [self setNeedsDisplay]; } #pragma mark 繪制界面(1.UIView初始化后自動(dòng)調(diào)用; 2.調(diào)用setNeedsDisplay方法時(shí)會(huì)自動(dòng)調(diào)用) - (void)drawRect:(CGRect)rect { // 重寫父類方法,首先要調(diào)用父類的方法 [super drawRect:rect]; //設(shè)置隨機(jī)背景顏色 self.backgroundColor = kRandomColor; //獲得要顯示驗(yàn)證碼字符串,根據(jù)長(zhǎng)度,計(jì)算每個(gè)字符顯示的大概位置 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; //依次繪制每一個(gè)字符,可以設(shè)置顯示的每個(gè)字符的字體大小、顏色、樣式等 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}]; } //調(diào)用drawRect:之前,系統(tǒng)會(huì)向棧中壓入一個(gè)CGContextRef,調(diào)用UIGraphicsGetCurrentContext()會(huì)取棧頂?shù)腃GContextRef CGContextRef context = UIGraphicsGetCurrentContext(); //設(shè)置畫線寬度 CGContextSetLineWidth(context, kLineWidth); //繪制干擾的彩色直線 for(int i = 0; i < kLineCount; i++) { //設(shè)置線的隨機(jī)顏色 UIColor *color = kRandomColor; CGContextSetStrokeColorWithColor(context, [color CGColor]); //設(shè)置線的起點(diǎn) pX = arc4random() % (int)rect.size.width; pY = arc4random() % (int)rect.size.height; CGContextMoveToPoint(context, pX, pY); //設(shè)置線終點(diǎn) pX = arc4random() % (int)rect.size.width; pY = arc4random() % (int)rect.size.height; CGContextAddLineToPoint(context, pX, pY); //畫線 CGContextStrokePath(context); } } @end
關(guān)于怎么在iOS中實(shí)現(xiàn)生成圖片數(shù)字字母驗(yàn)證效果就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。