IOS-验证码的实现和封装(可以直接调用)
最近对OC中的图像比较感兴趣。随手搞得一个类似验证码的demo。直接贴代码了。
小demo中的VerificationCodeView是继承自UIView的,所以需要用到的时候,可以直接定义一个UIView,addSubviews就可以了。还是挺方便的。哈哈😄
小demo中的#define kCharCount 表示显示几个验证字符的。这个自己可以根据自己的需要自己定义。
小弟刚接触图像绘制,不足之处,余生请多指教
Life-long learning ...
#define kRandomColor [UIColor colorWithRed:arc4random() % 256 / 256.0 green:arc4random() % 256 / 256.0 blue:arc4random() % 256 / 256.0 alpha:1.0]; #define kLineCount 6 #define kLineWidth 1.0 #define kCharCount 6 #define kFontSize [UIFont systemFontOfSize:arc4random() % 5 + 15] @implementation VerificationCodeView @synthesize verificationCodeStr,verificationCodeArray; - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { self.layer.cornerRadius = 5.0; self.layer.masksToBounds = YES; self.backgroundColor = kRandomColor; //显示一个随机验证码 [self changeCaptcha]; } return self; } //获取验证码 -(void)changeCaptcha { self.verificationCodeArray = [[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.verificationCodeStr = [[NSMutableString alloc] initWithCapacity:kCharCount]; for(int i = 0; i < kCharCount; i++) { NSInteger index = arc4random() % ([self.verificationCodeArray count] - 1); getStr = [self.verificationCodeArray objectAtIndex:index]; self.verificationCodeStr = (NSMutableString *)[self.verificationCodeStr stringByAppendingString:getStr]; } } -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { //切换验证码 [self changeCaptcha]; //setNeedsDisplay调用drawRect方法来实现view的绘制 [self setNeedsDisplay]; } - (void)drawRect:(CGRect)rect { [super drawRect:rect]; self.backgroundColor = kRandomColor; //获得要显示验证码字符串,根据长度,计算每个字符显示的大概位置 NSString *text = [NSString stringWithFormat:@"%@",self.verificationCodeStr]; 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; //依次绘制每一个字符,可以设置显示的每个字符的字体大小、颜色、样式等 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); } }
小弟,稍后会将源文件传到GitHub上。
站在巨人的肩膀上行走