国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

iOS生成圖片數字字母驗證效果

2019-10-21 18:39:18
字體:
來源:轉載
供稿:網友

本文實例為大家分享了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, pX, pY);      //畫線      CGContextStrokePath(context);    }}@end

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到IOS開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 六安市| 榆林市| 宝鸡市| 额敏县| 洛南县| 芦溪县| 宿州市| 鸡西市| 福贡县| 安康市| 浦江县| 潍坊市| 南召县| 齐河县| 汪清县| 合水县| 五河县| 伊金霍洛旗| 靖宇县| 汝州市| 莱州市| 阆中市| 繁昌县| 西安市| 博客| 霞浦县| 雅江县| 克山县| 巴彦县| 合水县| 灵寿县| 藁城市| 申扎县| 鹰潭市| 同仁县| 五家渠市| 周宁县| 奉贤区| 正蓝旗| 怀化市| 台前县|