溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

iOS中怎么生成圖片數(shù)字字母驗(yàn)證效果

發(fā)布時(shí)間:2021-08-11 14:00:09 來(lái)源:億速云 閱讀:93 作者:Leah 欄目:編程語(yǔ)言

這篇文章將為大家詳細(xì)講解有關(guān)iOS中怎么生成圖片數(shù)字字母驗(yàn)證效果,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。

#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:來(lái)設(shè)置,好處是當(dāng)元素個(gè)數(shù)不超過(guò)容量時(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)證碼可以直接寫(xiě)到這個(gè)方法中,不用再額外添加手勢(shì)-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{  //點(diǎn)擊界面,切換驗(yàn)證碼  [self changeCaptcha];  //setNeedsDisplay調(diào)用drawRect方法來(lái)實(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 {  // 重寫(xiě)父類方法,首先要調(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è)置畫(huà)線寬度    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);      //畫(huà)線      CGContextStrokePath(context);    }}@end

關(guān)于iOS中怎么生成圖片數(shù)字字母驗(yàn)證效果就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

ios
AI