簡單封裝了一個隨機獲取驗證碼;
#import@interface AuthcodeView : UIView
@property (strong, nonatomic) NSArray *dataArray;//字符素材數(shù)組
@property (strong, nonatomic) NSMutableString *authCodeStr;//驗證碼字符串
- (void)getAuthcode;
@end
#import "AuthcodeView.h"
#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 4
#define kFontSize [UIFont systemFontOfSize:arc4random() % 5 + 15]
@implementation AuthcodeView
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
self.layer.cornerRadius = 5.0f;
self.layer.masksToBounds = YES;
self.backgroundColor = kRandomColor;
[self getAuthcode];//獲得隨機驗證碼
}
return self;
}
#pragma mark 獲得隨機驗證碼
- (void)getAuthcode
{
//字符串素材
self.layer.cornerRadius = 3.0f;
self.layer.masksToBounds = YES;
self.backgroundColor = kRandomColor;
_dataArray = [[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];
_authCodeStr = [[NSMutableString alloc] initWithCapacity:kCharCount];
//隨機從數(shù)組中選取需要個數(shù)的字符串胎许,拼接為驗證碼字符串
for (int i = 0; i < kCharCount; i++)
{
NSInteger index = arc4random() % (_dataArray.count-1);
NSString *tempStr = [_dataArray objectAtIndex:index];
_authCodeStr = (NSMutableString *)[_authCodeStr stringByAppendingString:tempStr];
}
}
#pragma mark 點擊界面切換驗證碼
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self getAuthcode];
//setNeedsDisplay調(diào)用drawRect方法來實現(xiàn)view的繪制
[self setNeedsDisplay];
}
- (void)drawRect:(CGRect)rect
{
[super drawRect:rect];
//設(shè)置隨機背景顏色
self.backgroundColor = kRandomColor;
//根據(jù)要顯示的驗證碼字符串峻呛,根據(jù)長度罗售,計算每個字符串顯示的位置
NSString *text = [NSString stringWithFormat:@"%@",_authCodeStr];
CGSize cSize = [@"A" sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:20]}];
int width = rect.size.width/text.length - cSize.width;
int height = rect.size.height - cSize.height;
CGPoint point;
//依次繪制每一個字符,可以設(shè)置顯示的每個字符的字體大小、顏色钩述、樣式等
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}];
}
//調(diào)用drawRect:之前寨躁,系統(tǒng)會向棧中壓入一個CGContextRef,調(diào)用UIGraphicsGetCurrentContext()會取棧頂?shù)腃GContextRef
CGContextRef context = UIGraphicsGetCurrentContext();
//設(shè)置線條寬度
CGContextSetLineWidth(context, kLineWidth);
//繪制干擾線
for (int i = 0; i < kLineCount; i++)
{
UIColor *color = kRandomColor;
CGContextSetStrokeColorWithColor(context, color.CGColor);//設(shè)置線條填充色
//設(shè)置線的起點
pX = arc4random() % (int)rect.size.width;
pY = arc4random() % (int)rect.size.height;
CGContextMoveToPoint(context, pX, pY);
//設(shè)置線終點
pX = arc4random() % (int)rect.size.width;
pY = arc4random() % (int)rect.size.height;
CGContextAddLineToPoint(context, pX, pY);
//畫線
CGContextStrokePath(context);
}
}
@end