復(fù)制粘貼 歡樂(lè)就完了
1厌衔、 創(chuàng)建一個(gè)繼承與UIview的類名字叫XGAuthCode
2仿滔、XGAuthCode.h文件中復(fù)制以下代碼
@property (strong, nonatomic) NSMutableString *authCodeStr;//驗(yàn)證碼字符串
@property (strong, nonatomic) NSArray *dataArray;
- (void) getAuthcode;
3、XGAuthCode.m文件中復(fù)制以下代碼
#define kRandomColorAL [UIColor colorWithRed:arc4random() % 256 / 256.0 green:arc4random() % 256 / 256.0 blue:arc4random() % 256 / 256.0 alpha:0.5];
//干擾線隨機(jī)顏色
#define kRandomColor [UIColor colorWithRed:arc4random() % 256 / 256.0 green:arc4random() % 256 / 256.0 blue:arc4random() % 256 / 256.0 alpha:1];
//干擾線數(shù)量
#define kLineCount 4
//干擾線寬度
#define kLineWidth 1.0
//驗(yàn)證碼數(shù)量
#define kCharCount 4
//驗(yàn)證碼大小
#define kFontSize (arc4random() % 5 + 20)
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
self.layer.cornerRadius = 5.0f;
self.layer.masksToBounds = YES;
self.backgroundColor = kRandomColor;
[self getAuthcode];//獲得隨機(jī)驗(yàn)證碼
}
return self;
}
#pragma mark 獲得隨機(jī)驗(yàn)證碼
- (void)getAuthcode
{
//字符串素材
_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];
//隨機(jī)從數(shù)組中選取需要個(gè)數(shù)的字符串膝擂,拼接為驗(yàn)證碼字符串
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 點(diǎn)擊界面切換驗(yàn)證碼
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self getAuthcode];
//setNeedsDisplay調(diào)用drawRect方法來(lái)實(shí)現(xiàn)view的繪制
[self setNeedsDisplay];
}
#pragma mark 繪制
- (void)drawRect:(CGRect)rect
{
[super drawRect:rect];
//設(shè)置隨機(jī)背景顏色
self.backgroundColor = kRandomColorAL;
//根據(jù)要顯示的驗(yàn)證碼字符串亚斋,根據(jù)長(zhǎng)度,計(jì)算每個(gè)字符串顯示的位置
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;
//依次繪制每一個(gè)字符,可以設(shè)置顯示的每個(gè)字符的字體大小拂募、顏色庭猩、樣式等
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];
UIFontDescriptor *attributeFontDescriptor = [UIFontDescriptor fontDescriptorWithFontAttributes:
@{UIFontDescriptorFamilyAttribute: @"Marion",
UIFontDescriptorNameAttribute:@"HanziPenSC-W3",
UIFontDescriptorSizeAttribute: @40.0,
UIFontDescriptorMatrixAttribute:[NSValue valueWithCGAffineTransform:CGAffineTransformMakeRotation(M_1_PI*(arc4random() % 2 ))
]}];
UIFont * font = [UIFont fontWithDescriptor:attributeFontDescriptor size:kFontSize];
UIColor *color = kRandomColor;
[textC drawAtPoint:point withAttributes:@{NSFontAttributeName:font,NSForegroundColorAttributeName:color}];
}
//調(diào)用drawRect:之前,系統(tǒng)會(huì)向棧中壓入一個(gè)CGContextRef陈症,調(diào)用UIGraphicsGetCurrentContext()會(huì)取棧頂?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è)置線的起點(diǎn)
pX = arc4random() % (int)rect.size.width;
pY = arc4random() % (int)rect.size.height;
CGContextMoveToPoint(context, pX, pY);
//設(shè)置線終點(diǎn)
pX = arc4random() % (int)rect.size.width;
pY = arc4random() % (int)rect.size.height;
CGContextAddLineToPoint(context, pX, pY);
//畫線
CGContextStrokePath(context);
}
}
4蔼水、使用方法在需要使用的文件中導(dǎo)入 #import "XGAuthCode.h"
1、定義屬性:
@property (nonatomic, strong) XGAuthCode * authcodeImage; //< 驗(yàn)證碼視圖
2录肯、初始化:
self.authcodeImage = [[XGAuthCode alloc] initWithFrame:CGRectMake(XGWidth*0.5-60, 5*XGREM, 120, 60)];
[self.view addSubview:self.authcodeImage];
[self.authcodeImage getAuthcode];
NSLog(@"%@",self.authcodeImage.authCodeStr);
3趴腋、獲取驗(yàn)證碼:
NSLog(@"%@",self.authcodeImage.authCodeStr);
請(qǐng)根據(jù)項(xiàng)目中根據(jù)實(shí)際業(yè)務(wù)判斷邏輯將圖形驗(yàn)證添加到適當(dāng)?shù)奈恢眠M(jìn)行圖形驗(yàn)證
附:效果圖如下