iOS 驗(yàn)證碼的實(shí)現(xiàn)

我們經(jīng)常會(huì)在手機(jī)應(yīng)用中會(huì)看到輸入驗(yàn)證碼,然后操作一個(gè)東西,那么我們應(yīng)該如何去實(shí)現(xiàn)他呢,先看一下效果

verificationCode.gif

1、首先我們定義一個(gè)繼承UIView的類ZYCaptchaView用于、生成驗(yàn)證碼的操作
2、.h文件中我們這樣聲明

/**字符數(shù)組 */
@property (nonatomic , strong)NSArray *characterArray;
/** 驗(yàn)證碼字符串 */
@property (nonatomic , strong)NSMutableString  *captchaString;
 /** 加載驗(yàn)證碼 */
- (void)loadCaptcha;

3、.m文件中我們具體去實(shí)現(xiàn)
首先,我們?cè)?code>initWithFrame中生成一個(gè)隨機(jī)的驗(yàn)證碼

- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
    //設(shè)置layer圓角半徑
    self.layer.cornerRadius = 5.0;
    //影藏邊境
    self.layer.masksToBounds = YES;
    self.backgroundColor = kRandomColor;
    //顯示一個(gè)隨機(jī)驗(yàn)證碼
    [self randomCaptcha];
}
return self;
}
 #pragma mark -- 更換驗(yàn)證碼
- (void)randomCaptcha {
//數(shù)組中存放的是全部可選的字符,可以是字母,也可以是中文
self.characterArray = [[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];
NSMutableString *getStr = [[NSMutableString alloc] initWithCapacity:kCharCount];
self.captchaString = [[NSMutableString alloc] initWithCapacity:kCharCount];
//隨機(jī)從數(shù)組中選取需要個(gè)數(shù)的字符,然后拼接為一個(gè)字符串
for (int i = 0 ; i < kCharCount; i ++) {
    int index = arc4random() % (self.characterArray.count - 1);
    getStr = [self.characterArray objectAtIndex:index];
    self.captchaString = [[self.captchaString stringByAppendingString:getStr] copy];
}
//從網(wǎng)絡(luò)獲取字符串,然后把得到的字符串在本地繪制出來(lái)(網(wǎng)絡(luò)獲取步驟在這省略)
}

其次,我們實(shí)現(xiàn)加載驗(yàn)證碼的代碼

/** 加載驗(yàn)證碼 */
- (void)loadCaptcha {
   //更換驗(yàn)證碼
 [self randomCaptcha];
//重新繪制
[self setNeedsDisplay];
}

最后我們實(shí)現(xiàn)繪制驗(yàn)證碼的代碼,也就是重寫<code>drawRect</code>方法

#pragma mark 繪制界面
- (void)drawRect:(CGRect)rect {
[super drawRect:rect];
//設(shè)置隨機(jī)背景顏色
self.backgroundColor = kRandomColor;
//獲取要顯示的驗(yàn)證碼字符串
NSString *text = [NSString stringWithFormat:@"%@" , self.captchaString];
CGSize cSize = [@"s" sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:20]}];
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}];
}
//獲取上下文
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);
  }
}

4、在控制器中調(diào)用我們的類
需要注意的是我們需要讓驗(yàn)證碼不區(qū)分大小寫,我們就需要大小寫進(jìn)行轉(zhuǎn)換,你可以將所有字母轉(zhuǎn)換為大寫,也可以轉(zhuǎn)換為小寫。大小寫轉(zhuǎn)換的代碼如下:

 /** 大寫字母轉(zhuǎn)換為小寫 */
- (NSString *)toLower:(NSString *)str {
for (int i = 0; i < str.length; i++) {
    if ([str characterAtIndex:i] >= 'A' && [str characterAtIndex:i] <= 'Z') {
        char temp = [str characterAtIndex:i] + 32;
        NSRange range = NSMakeRange(i, 1);
        str = [str stringByReplacingCharactersInRange:range withString:[NSString stringWithFormat:@"%c" , temp]];
    }
}
return str;
}
/** 小寫字母轉(zhuǎn)換為大寫 */
-(NSString *)toUpper:(NSString *)str{
for (int i = 0; i < str.length; i++) {
    if ([str characterAtIndex:i]>='a'&[str characterAtIndex:i]<='z') {
        char  temp=[str characterAtIndex:i]-32;
        NSRange range=NSMakeRange(i, 1);
        str=[str stringByReplacingCharactersInRange:range withString:[NSString stringWithFormat:@"%c",temp]];
    }
}
return str;
}

大概就這些步驟了,如有需要可以下載:https://github.com/zhangyqyx/verificationCode
希望大家能提出寶貴的意見(jiàn),可以給我留言,也可以發(fā)郵件到我的郵箱:namezyqyx@163.com
謝謝大家,如果你有更好的想法或文章請(qǐng)告知,不勝感激。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見(jiàn)模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,697評(píng)論 19 139
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫(kù)、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,694評(píng)論 4 61
  • 體驗(yàn)入 與身邊的人相處,難免會(huì)有磕磕碰碰的時(shí)候。一次兩次,無(wú)傷大雅,說(shuō)開了就好;但如果整日里都處于爭(zhēng)鋒相對(duì)的狀態(tài),...
    Una笑笑閱讀 176評(píng)論 0 2
  • 一、原圖 二、Ctrl+J復(fù)制一個(gè)新圖層,位于原始圖層上方。 三、對(duì)復(fù)制的圖層使用濾鏡,并執(zhí)行一下步驟“濾鏡— 其...
    爾而知致閱讀 564評(píng)論 0 0
  • 常常講到業(yè)務(wù)模型可以簡(jiǎn)單分類為 產(chǎn)銷研,或者叫做技工貿(mào)也好,反正就是要整合各個(gè)環(huán)節(jié),以利于公司快速對(duì)市場(chǎng)反應(yīng),說(shuō)來(lái)...
    莫問(wèn)莊主閱讀 1,128評(píng)論 0 1

友情鏈接更多精彩內(nèi)容