直接上代码
// ZSYView.h
//
// ZSYView.h
// 本地随机验证码
//
// Created by ZSY on 17/8/3.
// Copyright © 2017年 ZSY. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface ZSYView : UIView
@property (nonatomic, strong) NSString *codeNum; // 验证码
// 刷新
- (void)refreshCode;
@end
// ZSYView.m
//
// ZSYView.m
// 本地随机验证码
//
// Created by ZSY on 17/8/3.
// Copyright © 2017年 ZSY. All rights reserved.
//
#import "ZSYView.h"
#define zsyRandomColor [UIColor colorWithRed:arc4random() % 256 / 256.0 green:arc4random() % 256 / 256.0 blue:arc4random() % 256 / 256.0 alpha:1.0];
#define zsyLineCount 4
#define zsyLineWidth 1.0
#define zsyCharCount 4
#define zsyFontSize [UIFont systemFontOfSize:arc4random() % 5 + 15]
@interface ZSYView ()
@end
@implementation ZSYView
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor whiteColor];
self.layer.cornerRadius = 5.0;
self.layer.masksToBounds = YES;
[self setup];
}
return self;
}
#pragma mark 刷新验证码
- (void)refreshCode {
[self setup];
[self setNeedsDisplay];
}
- (void)setup {
//数组中存放的是全部可选的字符,可以是字母,也可以是中文
NSArray *array = [NSArray arrayWithObjects:@"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];
self.codeNum = @"";
//随机从数组中选取需要个数的字符,然后拼接为一个字符串
for (int i = 0; i < zsyCharCount; i++) {
NSInteger index = arc4random() % (array.count - 1);
NSString *str = array[index];
self.codeNum = [self.codeNum stringByAppendingString:str];
}
}
#pragma mark 点击更换验证码
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[self refreshCode];
}
#pragma mark 绘制界面(1.UIView初始化后自动调用; 2.调用setNeedsDisplay方法时会自动调用)
- (void)drawRect:(CGRect)rect {
[super drawRect:rect];
//获得要显示验证码字符串,根据长度,计算每个字符显示的大概位置
NSString *text = self.codeNum;
CGSize size = [@"H" sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:20]}];
CGFloat width = rect.size.width / text.length - size.width;
CGFloat height = rect.size.height - size.height;
CGPoint point;
CGFloat x, y;
//依次绘制每一个字符,可以设置显示的每个字符的字体大小、颜色、样式等
for (int i = 0; i < text.length; i++) {
x = arc4random() % (int)width + rect.size.width / text.length * i;
y = arc4random() % (int)height;
point = CGPointMake(x, y);
unichar c = [text characterAtIndex:i];
NSString *stringC = [NSString stringWithFormat:@"%C", c];
UIColor *color = zsyRandomColor;
[stringC drawAtPoint:point withAttributes:@{NSFontAttributeName:zsyFontSize, NSForegroundColorAttributeName:color}];
}
//调用drawRect:之前,系统会向栈中压入一个CGContextRef,调用UIGraphicsGetCurrentContext()会取栈顶的CGContextRef
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, zsyLineWidth);
//绘制干扰的彩色直线
for (int i = 0; i < zsyLineCount; i++) {
UIColor *color = zsyRandomColor;
CGContextSetStrokeColorWithColor(context, color.CGColor);
// 起点
x = arc4random() % (int)rect.size.width;
y = arc4random() % (int)rect.size.height;
CGContextMoveToPoint(context, x, y);
// 终点
x = arc4random() % (int)rect.size.width;
y = arc4random() % (int)rect.size.height;
CGContextAddLineToPoint(context, x, y);
// 画线
CGContextStrokePath(context);
}
}
@end
使用
// ViewController.m
//
// ViewController.m
// 本地随机验证码
//
// Created by ZSY on 17/8/3.
// Copyright © 2017年 ZSY. All rights reserved.
//
#import "ViewController.h"
#import "ZSYView.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.view.backgroundColor = [UIColor lightGrayColor];
ZSYView *view = [[ZSYView alloc] initWithFrame:CGRectMake(100, 100, 100, 40)];
// view.backgroundColor = [UIColor blackColor];
view.tag = 100;
[self.view addSubview:view];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
for (UIView *v in self.view.subviews) {
if (v.tag == 100) {
ZSYView *view = (ZSYView *)v;
NSLog(@"%@", view.codeNum);
}
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end