- (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:datalength: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;
}

iOS随机生成验证码相关推荐

  1. html验证码功能如何实现原理,基于JS实现一个随机生成验证码功能

    效果展示 实现原理 1. html:一般就是一个div: 2. JS:1)将所有的验证码所用的字符放在一个字符串中 2)在这个字符串的字符个数以内,随机生成索引号 3)根据索引号查找对应字符,拼接成验 ...

  2. 随机生成验证码及发送短信验证码

    验证码 包括过期时间和redis缓存以及校验 1.设置过期时间 private Date getNextDayZeroTime() {Calendar calendar = Calendar.getI ...

  3. 随机验证码如何在html里设置颜色,js随机生成验证码以及随机颜色

    Javascript通过Math.random()随机生成验证码. 代码如下: 随机验证码 .p1{ width:100px; height:30px; border:1px solid black; ...

  4. php随机生成验证码,php随机生成数字,php随机生成数字加字母!

    <?php /** 方法类* */ class functions {/*** PHP随机生成验证码函数** @param array* @return mixed*/function rand ...

  5. php随机生成验证码代码

    <?php session_start(); //产生一个随机的字符串验证码 $checkcode=""; for ($i=0;$i<4;$i++){$checkcod ...

  6. python随机生成验证码_Python生成随机验证码的两种方法

    # -*- coding: utf-8 -*- import random def generate_verification_code_v2(): ''' 随机生成6位的验证码 ''' code_l ...

  7. Java随机生成验证码

    import java.util.Random;public class Test4 {public static void main(String[] args) {/*生成验证码内容:可以是小写字 ...

  8. 前端js——验证码登录(canvas画布),随机生成验证码,判断正确性

    1.html canvas画布: 元素用于图形的绘制,canvas 元素本身是没有绘图能力的,通过脚本 (通常是JavaScript)来完成. 标签通常需要指定一个id属性 (脚本中经常引用), wi ...

  9. python随机生成验证码_Python生成随机验证码

    #生成随机验证码 from PIL importImagefrom io importBytesIOfrom PIL importImageDraw,ImageFontdefcheck_code(re ...

最新文章

  1. WSRP调用中的一些问题
  2. 学习js,尝试写一个表单验证框架(1)-规划
  3. dispatch作用 react_「React系列」手把手带你撸后台系统(Redux与路由鉴权)
  4. uniapp对象怎么获取值_第65p,关于:生成器、迭代器与迭代对象(上)
  5. pscs6插件_PS插件-灯光工厂
  6. VTK 曲面构建+颜色映射
  7. java bean 工厂模式_深入理解Java的三种工厂模式
  8. Reactive Extensions 相见恨晚的Rx.Net
  9. tt协议号服务器,TTIot: TTIoT云端物联网Iot组件;面向JAVA;netty;mqtt;异步推送;以事件为驱动;为设备提供安全可靠的连接通信能力;...
  10. python购物信息整合教程_python实现简单购物商城
  11. 谷歌提出新型自动语音识别数据增强大法,直接对频谱图“动刀”,提升模型表现...
  12. Python入门--with语句
  13. 解决Docker for window与VMware虚拟机同时安装,造成虚拟机网络不通以及无法启动问题...
  14. 老男孩教育每日一题-2017年4月10日-find命令题目
  15. 制作U盘启动盘 优启通
  16. 数说CS|中国人民大学高瓴人工智能学院保研生源大起底!
  17. Ivar Jacobson 先生简介
  18. MFC加载gif动态图片的方法
  19. Java创建学生喂养动物类
  20. 我是一个坚持“朝九晚五”的程序员

热门文章

  1. RPC框架的路由策略
  2. 计算机组装win7实训报告,计算机基础1实验报告win7基本操作.docx
  3. 计算机制图基本知识6,机械制图的基础知识,看完小白也懂了!
  4. 云应用程序服务器错误怎么办,“/”应用程序中的服务器错误解决方法
  5. 每日实用技巧分享:怎么修复老照片?
  6. 程序员要从“美团崩溃”中学习的痛点
  7. 怎么修复录音笔(设备)损坏的WAV或MP3文件0字节文件
  8. Helix MP3解码库脱离汇编指令束缚,运行在任何处理器上的解决方案
  9. 四川店盈通电子商务有限公司:十荟团热潮退却,泡沫消失
  10. 女博士分享读博经历成“网红”的1年后,却被“网暴”到去公安局立案.....