一個簡單的驗證碼工具

✨嘿o大遠發表於2017-11-23

首先反省一下這個不是重點.
好久沒寫文章了,兩點原因
1.最近換了一份新工具,在比較忙碌
2.個人原因比較懶散,希望自制力強一些

iOS 本地驗證碼生成工具

其實這個工具很久之前就已經跟朋友一起弄好了現在才釋出到cocoapods,這個驗證碼工具極其簡單使用drawRect繪製到View上 下面我來說一下技術點(其實沒啥說的原始碼一幕瞭然)

1.生成字串

+ (instancetype)randomCaptchaStringWithLength:(NSUInteger)length {
    int i = 0;
    char *str = (char*)malloc(length*sizeof(char));
    do {
        int path = arc4random_uniform(74) + 48;
        if ((47 < path && path < 58) ||
            (64 < path && path < 91) ||
            (96 < path && path < 123)) {
            str[i] = path;
            i++;
        }
    } while (i < length);
    NSString *retString = [NSString stringWithCString:str encoding:NSUTF8StringEncoding];
    free(str);
    return retString;

}複製程式碼

程式碼如上利用ASCII生成指定長度的隨機字串,這裡本人覺得這麼寫思路還可以當然弄個陣列然後取值也是一個不錯的辦法.

2.繪製

- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    //計算當前字串長度還有檢視大小,獲得寬高
    CGFloat itemWidth = CGRectGetWidth(rect) / self.captchaString.length;
    CGFloat itemHeight = CGRectGetHeight(rect);
    for (int i = 0; i < self.captchaString.length; i++) {
        unichar character = [self.captchaString characterAtIndex:i];
        NSString *characterString = [NSString stringWithFormat:@"%C", character];
        //字號
        CGFloat fontSize = arc4random_uniform(itemHeight * .3f) + itemHeight * .4f;        
        //字元屬性
        NSMutableDictionary *attributes = [NSMutableDictionary dictionaryWithObject:[UIFont systemFontOfSize:fontSize] forKey:NSFontAttributeName];

        if (self.isItalic) {
            //字型傾斜
            int direction = arc4random_uniform(2);
            CGFloat obliqueness = arc4random_uniform(5) / 10.f;
            [attributes setObject:@(obliqueness * (direction ? 1 : -1)) forKey:NSObliquenessAttributeName];
        }

        //字元寬度
        CGRect characterRect = [characterString boundingRectWithSize:CGSizeMake(itemWidth, itemHeight)
                                                             options:NSStringDrawingUsesLineFragmentOrigin
                                                          attributes:attributes
                                                             context:nil];
        CGFloat characterWidth = CGRectGetWidth(characterRect);

        //x軸居中,y軸隨機
        CGFloat x = (itemWidth - characterWidth) / 2.f + itemWidth * i;
        CGFloat y = arc4random_uniform(itemHeight - fontSize);

        [characterString drawAtPoint:CGPointMake(x, y) withAttributes:attributes];
    }
    //畫干擾線
    for (int i = 0; i < self.captchaString.length; i++) {
        CGContextRef ctx = UIGraphicsGetCurrentContext();
        CGFloat x = arc4random_uniform(CGRectGetMidX(rect));
        CGFloat y = arc4random_uniform(CGRectGetHeight(rect));
        CGFloat destX = arc4random_uniform(CGRectGetMidX(rect)) + CGRectGetMidX(rect);
        CGFloat destY = arc4random_uniform(CGRectGetHeight(rect));
        CGContextMoveToPoint(ctx, x, y);
        CGContextSetLineWidth(ctx, .5f);
        CGContextSetRGBStrokeColor(ctx,
                                   arc4random_uniform(256) / 255.f,
                                   arc4random_uniform(256) / 255.f,
                                   arc4random_uniform(256) / 255.f,
                                   1.f);
        CGContextAddLineToPoint(ctx, destX, destY);
        CGContextStrokePath(ctx);
    }
}複製程式碼

其實原始碼還是很簡單的主要是想把工具分享給大家,還希望能夠幫助到大家
最後github地址奉上 使用方法github見
TLImageCaptchaView

相關文章