- (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. 小花梨的三角形(暴力上下扫三角形)
  2. MyBatis的功能架构是怎样的
  3. c# out关键字 vb_在c#中使用out关键字
  4. java 通道 双向原理_Java-NIO(四):通道(Channel)的原理与获取
  5. 巴菲特:马斯克是伟大企业家,但特斯拉股票不是好投资标的
  6. 阶段1 语言基础+高级_1-2 -面向对象和封装_11使用对象类型作为方法的返回值
  7. 数据库零碎要点002---认识时序数据库
  8. Java自动生成增量补丁自动部署_Shell脚本结合Git实现增量项目部署
  9. Windows 多功能计算器
  10. C++:关于类以及h/cpp文件的一些实用知识
  11. 服务器上修改websphere变量,WebSphere常用设置
  12. Ubuntu 论坛遭到入侵,用户数据泄露
  13. 用pip freeze >requirements.txt命令迁移模块
  14. js两只手指控制div图片放大缩小功能(2)
  15. 聊一聊什么是SaaS,以及遇到的问题......
  16. 转载:Vsphere 出现 “ XXX esx.problem.hyperthreading.unmitigated.formatOnHost not found XXX”的解决方案
  17. 例25:求1+2!+3!+...+20!的和。
  18. SNAT DNAT
  19. 期货开户不同客户手续费不同
  20. 最长的名字 /比较最长的字符串并输出

热门文章

  1. MTK平台修改Android动画,Android MTK平台修改开关机动画和开机logo
  2. 21天学通c语言思维导图,21天思维导图学习 ----开发脑洞、助力成长
  3. 《Spring实战》读书笔记_装配bean
  4. python局部静态变量_python如何设置静态变量
  5. 揭秘返利APP了领取优惠就的背后隐秘
  6. 南理工计算机学院吉祥物,我们想要一个吉祥物,就差一个设计者了!
  7. 陈景润4篇代表性数学论文
  8. 写给不会爱自己的人,我自己
  9. 外企 国企 私企比较
  10. 送给单身猿们的表白神器