iOS使用UIView绘制本地验证码
Posted 轨迹
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS使用UIView绘制本地验证码相关的知识,希望对你有一定的参考价值。
记录一笔:
绘制本地验证码View的一个Demo源码:(出处:http://www.cnblogs.com/jerehedu/p/4527707.html)
.h文件的代码:
1 #import <UIKit/UIKit.h> 2 3 @interface AuthCodeView : UIView 4 5 @property (nonatomic, retain) NSMutableString *changeString; //验证码的字符串 6 @end
.m文件的代码:
1 #import "AuthCodeView.h" 2 3 #define kRandomColor [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1.0]; 4 #define kLineCount 6 5 #define kLineWidth 1.0 6 #define kCharCount 4 7 #define kFontSize [UIFont systemFontOfSize:arc4random() % 5 + 18] 8 9 @implementation AuthCodeView 10 @synthesize changeString; 11 12 - (instancetype)initWithFrame:(CGRect)frame 13 { 14 if (self = [super initWithFrame:frame]) { 15 16 self.layer.cornerRadius = 5.0; //设置layer圆角半径 17 self.layer.masksToBounds = YES; //隐藏边界 18 self.backgroundColor = kRandomColor; 19 20 //显示一个随机验证码 21 [self refreshCode]; 22 } 23 24 return self; 25 } 26 #pragma mark 更换验证码,得到更换的验证码的字符串 27 -(void)refreshCode { 28 //从字符数组中随机抽取相应数量的字符,组成验证码字符串 29 NSArray *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",nil]; 30 31 //如果能确定最大需要的容量,使用initWithCapacity:来设置,好处是当元素个数不超过容量时,添加元素不需要重新分配内存 32 NSMutableString *getStr = [[NSMutableString alloc] initWithCapacity:kCharCount]; 33 self.changeString = [[NSMutableString alloc] initWithCapacity:kCharCount]; 34 35 //随机从数组中选取需要个数的字符,然后拼接为一个字符串 36 for(int i = 0; i < kCharCount; i++) 37 { 38 NSInteger index = arc4random() % ([changeArray count] - 1); 39 getStr = [changeArray objectAtIndex:index]; 40 41 self.changeString = (NSMutableString *)[self.changeString stringByAppendingString:getStr]; 42 } 43 } 44 45 #pragma mark 点击view时调用,因为当前类自身就是UIView,点击更换验证码可以直接写到这个方法中,不用再额外添加手势 46 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 47 { 48 //点击界面,切换验证码 49 [self refreshCode]; 50 51 //setNeedsDisplay调用drawRect方法来实现view的绘制 52 [self setNeedsDisplay]; 53 } 54 55 #pragma mark 绘制界面(1.UIView初始化后自动调用; 2.调用setNeedsDisplay方法时会自动调用) 56 - (void)drawRect:(CGRect)rect { 57 // 重写父类方法,首先要调用父类的方法 58 [super drawRect:rect]; 59 60 //设置随机背景颜色 61 self.backgroundColor = kRandomColor; 62 63 //获得要显示验证码字符串,根据长度,计算每个字符显示的大概位置 64 NSString *text = [NSString stringWithFormat:@"%@",self.changeString]; 65 CGSize cSize = [@"S" sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:20]}]; 66 int width = rect.size.width / text.length - cSize.width; 67 int height = rect.size.height - cSize.height; 68 CGPoint point; 69 70 //依次绘制每一个字符,可以设置显示的每个字符的字体大小、颜色、样式等 71 float pX, pY; 72 for (int i = 0; i < text.length; i++) 73 { 74 pX = arc4random() % width + rect.size.width / text.length * i; 75 pY = arc4random() % height; 76 point = CGPointMake(pX, pY); 77 unichar c = [text characterAtIndex:i]; 78 NSString *textC = [NSString stringWithFormat:@"%C", c]; 79 80 [textC drawAtPoint:point withAttributes:@{NSFontAttributeName:kFontSize}]; 81 } 82 83 //调用drawRect:之前,系统会向栈中压入一个CGContextRef,调用UIGraphicsGetCurrentContext()会取栈顶的CGContextRef 84 CGContextRef context = UIGraphicsGetCurrentContext(); 85 //设置画线宽度 86 CGContextSetLineWidth(context, kLineWidth); 87 88 //绘制干扰的彩色直线 89 for(int i = 0; i < kLineCount; i++) 90 { 91 //设置线的随机颜色 92 UIColor *color = kRandomColor; 93 CGContextSetStrokeColorWithColor(context, [color CGColor]); 94 //设置线的起点 95 pX = arc4random() % (int)rect.size.width; 96 pY = arc4random() % (int)rect.size.height; 97 CGContextMoveToPoint(context, pX, pY); 98 //设置线终点 99 pX = arc4random() % (int)rect.size.width; 100 pY = arc4random() % (int)rect.size.height; 101 CGContextAddLineToPoint(context, pX, pY); 102 //画线 103 CGContextStrokePath(context); 104 } 105 } 106 107 @end
如何使用:
1 //显示验证码界面 2 AuthCodeView *codeView = [[AuthCodeView alloc] initWithFrame:CGRectMake(20, 40, 150, 40)]; 3 [self.view addSubview:codeView];
以上是关于iOS使用UIView绘制本地验证码的主要内容,如果未能解决你的问题,请参考以下文章