当前位置:首页 > 操作系统 > Ios

【iOS】使用UIView绘制本地验证码

记录一笔:

绘制本地验证码View的一个Demo源码:(出处:http://www.cnblogs.com/jerehedu/p/4527707.html)

.h文件的代码:

            1
            #import <UIKit/UIKit.h>
23@interface AuthCodeView : UIView
45 @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{
 14if (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 24return 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//随机从数组中选取需要个数的字符,然后拼接为一个字符串 36for(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]}];
 66int width = rect.size.width / text.length - cSize.width;
 67int height = rect.size.height - cSize.height;
 68    CGPoint point;
 69 70//依次绘制每一个字符,可以设置显示的每个字符的字体大小、颜色、样式等 71float pX, pY;
 72for (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//绘制干扰的彩色直线 89for(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}
106107@end

如何使用:

            1
            //
            显示验证码界面
            2 AuthCodeView *codeView = [[AuthCodeView alloc] initWithFrame:CGRectMake(20, 40, 150, 40)];
3 [self.view addSubview:codeView];

 

原文:http://www.cnblogs.com/vokie/p/5356110.html


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!