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;
}

目录
相关文章
|
26天前
|
API 数据安全/隐私保护 iOS开发
利用uni-app 开发的iOS app 发布到App Store全流程
利用uni-app 开发的iOS app 发布到App Store全流程
83 3
|
3月前
|
存储 iOS开发
iOS 开发,如何进行应用的本地化(Localization)?
iOS 开发,如何进行应用的本地化(Localization)?
122 2
|
3月前
|
存储 数据建模 数据库
IOS开发数据存储:什么是 UserDefaults?有哪些替代方案?
IOS开发数据存储:什么是 UserDefaults?有哪些替代方案?
38 0
|
3月前
|
API 定位技术 iOS开发
IOS开发基础知识:什么是 Cocoa Touch?它在 iOS 开发中的作用是什么?
IOS开发基础知识:什么是 Cocoa Touch?它在 iOS 开发中的作用是什么?
42 2
|
3月前
|
安全 编译器 Swift
IOS开发基础知识: 对比 Swift 和 Objective-C 的优缺点。
IOS开发基础知识: 对比 Swift 和 Objective-C 的优缺点。
89 2
|
3月前
|
API 开发工具 iOS开发
iOS 开发高效率工具包:10 大必备工具
iOS 开发高效率工具包:10 大必备工具
42 1
|
3月前
|
API 数据安全/隐私保护 iOS开发
利用uni-app 开发的iOS app 发布到App Store全流程
利用uni-app 开发的iOS app 发布到App Store全流程
52 1
|
2月前
|
监控 API Swift
用Swift开发iOS平台上的上网行为管理监控软件
在当今数字化时代,随着智能手机的普及,人们对于网络的依赖日益增加。然而,对于一些特定场景,如家庭、学校或者企业,对于iOS设备上的网络行为进行管理和监控显得尤为重要。为了满足这一需求,我们可以利用Swift语言开发一款iOS平台上的上网行为管理监控软件。
181 2
|
3月前
|
数据可视化 iOS开发
iOS 开发,什么是 Interface Builder(IB)?如何使用 IB 构建用户界面?
iOS 开发,什么是 Interface Builder(IB)?如何使用 IB 构建用户界面?
40 4
|
3月前
|
iOS开发
iOS开发解释 App 生命周期,包括各个阶段的调用顺序。
iOS开发解释 App 生命周期,包括各个阶段的调用顺序。
26 1