图形验证码

图形验证码

920 发布: 2024/11/20 20:06 本文总阅读量

使用方法

YBWeakSelf;
_captchaView = [[RKCaptchaView alloc]initWithFrame:CGRectMake(0, 0, 160, 60) andCharCount:4 andLineCount:4];
// 初始验证码
_showCaptchaStr = _captchaView.curCaptcharStr;
// 更新后
_captchaView.captchaEvent = ^(NSString *captchaStr) {
    weakSelf.showCaptchaStr = captchaStr;
};
[_imgCodeView addSubview:_captchaView];

源码

.h

#import <UIKit/UIKit.h>

typedef void(^CaptchaBlock)(NSString *captchaStr);

@interface RKCaptchaView : UIView

@property(nonatomic,strong)NSString *curCaptcharStr;
@property (nonatomic, copy) CaptchaBlock captchaEvent;

- (instancetype)initWithFrame:(CGRect)frame andCharCount:(NSInteger)charCount andLineCount:(NSInteger)lineCount;
- (void)resetCaptcha;

@end

.m

#import "RKCaptchaView.h"

#define CaptchaRandomColor [UIColor colorWithRed:arc4random() % 100 / 100.0 green:arc4random() % 100 / 100.0 blue:arc4random() % 100 / 100.0 alpha: 0.5]

@interface RKCaptchaView()

@property (nonatomic, copy) NSArray *charArray;
@property (nonatomic, strong) NSMutableString *charString;
/// 字符串数量
@property (nonatomic, assign) NSInteger charCount;
/// 线条数量
@property (nonatomic, assign) NSInteger lineCount;

@end


@implementation RKCaptchaView

- (instancetype)initWithFrame:(CGRect)frame andCharCount:(NSInteger)charCount andLineCount:(NSInteger)lineCount {
    if (self = [super initWithFrame:frame]) {
        self.charCount = charCount;
        self.lineCount = lineCount;
        self.layer.cornerRadius = 5.0;
        self.layer.masksToBounds = YES;
        self.backgroundColor = [UIColor whiteColor];//CaptchaRandomColor;
        [self changeCaptcha];
        [self addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(resetCaptcha)]];
    }
    return self;
}

- (void)resetCaptcha {
    [self changeCaptcha];
    [self setNeedsDisplay];
}

#pragma mark - 换一张
- (void)changeCaptcha {
    NSMutableString *getStr = [[NSMutableString alloc] initWithCapacity:self.charCount];
    self.charString = [[NSMutableString alloc] initWithCapacity:self.charCount];
    
    for(NSInteger i = 0; i < self.charCount; i++) {
        NSInteger index = arc4random() % ([self.charArray count]);
        getStr = [self.charArray objectAtIndex:index];
        self.charString = (NSMutableString *)[self.charString stringByAppendingString:getStr];
    }
    _curCaptcharStr = [NSString stringWithFormat:@"%@",_charString];
    if (self.captchaEvent) {
        self.captchaEvent(_curCaptcharStr);
    }
}

- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    self.backgroundColor = [UIColor whiteColor];//CaptchaRandomColor;
    CGFloat rectWidth = rect.size.width;
    CGFloat rectHeight = rect.size.height;
    CGFloat pointX, pointY;
    
    NSString *text = [NSString stringWithFormat:@"%@",self.charString];
    NSInteger charWidth = rectWidth / text.length - 15;
    NSInteger charHeight = rectHeight - 25;
    
    // 依次绘制文字
    for (NSInteger i = 0; i < text.length; i++) {
        // 文字X坐标
        pointX = arc4random() % charWidth + rectWidth / text.length * i;
        // 文字Y坐标
        pointY = arc4random() % charHeight;
        unichar charC = [text characterAtIndex:i];
        NSString *textC = [NSString stringWithFormat:@"%C", charC];

        [textC drawAtPoint:CGPointMake(pointX, pointY) withAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:arc4random() % 10 + 18]}];
    }
    
    // 获取上下文
    CGContextRef context = UIGraphicsGetCurrentContext();
    // 设置线宽
    CGContextSetLineWidth(context, 1.0);
    
    // 依次绘制直线
    for(NSInteger i = 0; i < self.lineCount; i++) {
        // 设置线的颜色
        CGContextSetStrokeColorWithColor(context, CaptchaRandomColor.CGColor);
        // 设置线的起点
        pointX = arc4random() % (NSInteger)rectWidth;
        pointY = arc4random() % (NSInteger)rectHeight;
        CGContextMoveToPoint(context, pointX, pointY);
        // 设置线的终点
        pointX = arc4random() % (NSInteger)rectWidth;
        pointY = arc4random() % (NSInteger)rectHeight;
        CGContextAddLineToPoint(context, pointX, pointY);
        // 绘画路径
        CGContextStrokePath(context);
    }
}

- (NSArray *)charArray {
    if (!_charArray) {
        // _charArray = [[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];
        _charArray = [[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",@"w",@"t",@"u",@"v",@"w",@"x",@"y",@"z",nil];
    }
    return _charArray;
}

@end