iOS随机生成验证码

- (void)onTapToGenerateCode:(UITapGestureRecognizer *)tap {
  for (UIView *view in self.checkCodeNumberLabel.subviews) {
    [view removeFromSuperview];
  }
  // @{
  // @name 生成背景色
  float red = arc4random() % 100 / 100.0;
  float green = arc4random() % 100 / 100.0;
  float blue = arc4random() % 100 / 100.0;
  UIColor *color = [UIColor colorWithRed:red green:green blue:blue alpha:0.2];
  [self.checkCodeNumberLabel setBackgroundColor:color];
  // @} end 生成背景色
  
  // @{
  // @name 生成文字
  const int count = 5;
  char data[count];
  for (int x = 0; x < count; x++) {
    int j = '0' + (arc4random_uniform(75));
    if((j >= 58 && j <= 64) || (j >= 91 && j <= 96)){
      --x;
    }else{
      data[x] = (char)j;
    }
  }
  NSString *text = [[NSString alloc] initWithBytes:data
                                            length:count encoding:NSUTF8StringEncoding];
  self.code = text;
  // @} end 生成文字
  
  CGSize cSize = [@"S" sizeWithFont:[UIFont systemFontOfSize:16]];
  int width = self.checkCodeNumberLabel.frame.size.width / text.length - cSize.width;
  int height = self.checkCodeNumberLabel.frame.size.height - cSize.height;
  CGPoint point;
  float pX, pY;
  for (int i = 0, count = text.length; i < count; i++) {
    pX = arc4random() % width + self.checkCodeNumberLabel.frame.size.width / text.length * i - 1;
    pY = arc4random() % height;
    point = CGPointMake(pX, pY);
    unichar c = [text characterAtIndex:i];
    UILabel *tempLabel = [[UILabel alloc]
                          initWithFrame:CGRectMake(pX, pY,
                                                   self.checkCodeNumberLabel.frame.size.width / 4,
                                                   self.checkCodeNumberLabel.frame.size.height)];
    tempLabel.backgroundColor = [UIColor clearColor];
    
    // 字体颜色
    float red = arc4random() % 100 / 100.0;
    float green = arc4random() % 100 / 100.0;
    float blue = arc4random() % 100 / 100.0;
    UIColor *color = [UIColor colorWithRed:red green:green blue:blue alpha:1.0];

    NSString *textC = [NSString stringWithFormat:@"%C", c];
    tempLabel.textColor = color;
    tempLabel.text = textC;
    [self.checkCodeNumberLabel addSubview:tempLabel];
  }
  
  // 干扰线
  CGContextRef context = UIGraphicsGetCurrentContext();
  CGContextSetLineWidth(context, 1.0);
  for(int i = 0; i < count; i++) {
    red = arc4random() % 100 / 100.0;
    green = arc4random() % 100 / 100.0;
    blue = arc4random() % 100 / 100.0;
    color = [UIColor colorWithRed:red green:green blue:blue alpha:1.0];
    CGContextSetStrokeColorWithColor(context, [color CGColor]);
    pX = arc4random() % (int)self.checkCodeNumberLabel.frame.size.width;
    pY = arc4random() % (int)self.checkCodeNumberLabel.frame.size.height;
    CGContextMoveToPoint(context, pX, pY);
    pX = arc4random() % (int)self.checkCodeNumberLabel.frame.size.width;
    pY = arc4random() % (int)self.checkCodeNumberLabel.frame.size.height;
    CGContextAddLineToPoint(context, pX, pY);
    CGContextStrokePath(context);
  }
  return;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值