随机验证码的生成

// 随机颜色
- (UIColor *)randomColor {
    NSInteger aRedValue   = arc4random() % 255;
    NSInteger aGreenValue = arc4random() % 255;
    NSInteger aBlueValue  = arc4random() % 255;
    UIColor *randColor    = [UIColor colorWithRed:aRedValue / 255.0f green:aGreenValue / 255.0f blue:aBlueValue / 255.0f alpha:1.0f];
    return randColor;
}

// 随机数
-(NSString*)randomCode{
    //数字: 48-57
    //小写字母: 97-122
    //大写字母: 65-90
    char chars[] = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLOMNOPQRSTUVWXYZ";
    char codes[self.count];

    for(int i=0; i<self.count; i++){
        codes[i]= chars[arc4random()%62];
    }

    NSString *text = [[NSString alloc] initWithBytes:codes
                                              length:self.count encoding:NSUTF8StringEncoding];
    return text;
}

// 画图
-(void)drawRect:(CGRect)rect{
   [super drawRect:rect];
    CGContextRef context = UIGraphicsGetCurrentContext();

    NSString *text = [self randomCode];
    //生成文字
    if (_didChangeCode) {
        _didChangeCode(text);
    }
    CGSize charSize =  [@"A" sizeWithAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:20.0]}];

    CGPoint point;
    float pointX, pointY;

    // text.length = 5
    int width = self.frame.size.width / text.length - charSize.width;
    int height = self.frame.size.height - charSize.height;

    //验证码的位置
    for (int i = 0; i < text.length; i++) {

        pointX = arc4random() % width + self.frame.size.width / text.length * i;
        pointY = arc4random() % height;

        point = CGPointMake(pointX, pointY);
        unichar c = [text characterAtIndex:i];
        NSString *textChar = [NSString stringWithFormat:@"%C", c];
        CGContextSetLineWidth(context, 1.0);
        //[[UIColor blueColor] set];
        float fontSize = (arc4random() % 10) + 17;

        [textChar drawAtPoint:point withAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:fontSize],NSStrokeColorAttributeName:[UIColor redColor],NSForegroundColorAttributeName:[self randomColor]}];
    }

    //干扰线
    CGContextSetLineWidth(context, 1.0);
    for(int i = 0; i < self.count; i++) {
        CGContextSetStrokeColorWithColor(context, [[self randomColor] CGColor]);
        pointX = arc4random() % (int)self.frame.size.width;
        pointY = arc4random() % (int)self.frame.size.height;
        CGContextMoveToPoint(context, pointX, pointY);
        pointX = arc4random() % (int)self.frame.size.width;
        pointY = arc4random() % (int)self.frame.size.height;
        CGContextAddLineToPoint(context, pointX, pointY);
        CGContextStrokePath(context);
    }
    //干扰点
    for(int i = 0;i < self.count*6;i++) {
        CGContextSetStrokeColorWithColor(context, [[self randomColor] CGColor]);
        pointX = arc4random() % (int)self.frame.size.width;
        pointY = arc4random() % (int)self.frame.size.height;
        CGContextMoveToPoint(context, pointX, pointY);
        CGContextAddLineToPoint(context, pointX+1, pointY+1);

        //CGContextFillRect(context, CGRectMake(pointX,pointY,1,1));
        CGContextStrokePath(context);

    }   
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值