宽容他人,放过自己。

验证码的生成

Posted on By anchoriteFili

相关引用


基本原理


1. 将要显示的字符、数字、特殊字符等存入到一个数组中。
2. 随机从数组中获取规定个数的字符并将其丢入到属性字符串中。
3. 一定范围你设置属性字符串中的每个字的x与y。
4. 设置属性字符串的字体大小、左右倾斜度,形成验证码。

相关代码

//
//  AuthcodeView.h
//  验证码生成view
//
//  Created by 赵宏亚 on 2017/8/11.
//  Copyright © 2017年 赵宏亚. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface AuthcodeView : UIView

@property (strong, nonatomic) NSArray *dataArray;//字符素材数组

@property (strong, nonatomic) NSMutableString *authCodeStr;//验证码字符串

@end

//
//  AuthcodeView.m
//  CaptchaTest
//
//  Created by 赵宏亚 on 2017/8/11.
//  Copyright © 2017年 赵宏亚. All rights reserved.
//

#import "AuthcodeView.h"

#define kCharCount 4 // 验证码显示的字数
#define kFontSize [UIFont systemFontOfSize:18] // 字号
@implementation AuthcodeView
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.layer.borderWidth = 1; // 边框宽度
        self.layer.borderColor = [[UIColor darkGrayColor] CGColor];
        self.layer.cornerRadius = 2.0f;
        self.layer.masksToBounds = YES;
        self.backgroundColor = [UIColor clearColor];
        [self getAuthcode];//获得随机验证码
    }
    return self;
}

#pragma mark 获得随机验证码
- (void)getAuthcode
{
    //字符串素材(只有大写字母)
    _dataArray = [[NSArray alloc] initWithObjects:@"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];
    //随机从数组中选取需要个数的字符串,拼接为验证码字符串
    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调用drawRect方法来实现view的绘制
    [self setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect
{
    [super drawRect:rect];
    
    //根据要显示的验证码字符串,根据长度,计算每个字符串显示的位置
    NSString *text = [NSString stringWithFormat:@"%@",_authCodeStr];
    
    CGSize cSize = [@"A" sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:18]}];
    
    int height = rect.size.height - cSize.height;
    CGPoint point;
    
    //依次绘制每一个字符,可以设置显示的每个字符的字体大小、颜色、样式等
    float pX,pY;
    for ( int i = 0; i<text.length; i++)
    {
        pX = (rect.size.width-3)/text.length * i+3;
        pY = arc4random() % (height-5) + 2.5;
        point = CGPointMake(pX, pY);
        
        unichar c = [text characterAtIndex:i];
        NSString *textC = [NSString stringWithFormat:@"%C", c];
        // NSObliquenessAttributeName 设置字倾斜角度,负值左,正值右
        [textC drawAtPoint:point withAttributes:@{NSFontAttributeName:kFontSize,NSObliquenessAttributeName:@(0.5-(arc4random()%100)/100.0)}];
    }

}


@end