iOS随机生成验证码
来源:互联网 发布:win7网络图标是灰色的 编辑:程序博客网 时间:2024/05/16 19:22
- (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 0
- iOS随机生成验证码
- iOS随机生成验证码
- ios学习之 生成随机验证码
- 生成随机验证码
- 随机生成验证码
- 生成随机验证码
- 随机生成验证码
- 生成随机验证码
- 生成随机验证码
- 生成随机验证码
- 生成随机验证码
- 随机生成验证码
- 随机生成验证码
- 随机生成验证码
- 随机生成验证码
- 随机生成验证码
- 生成随机验证码
- 随机生成验证码
- hdfs fsck 发生异常
- CSDN排名第一和第二的人
- linux下vi配置问题
- web.xml中load-on-startup的作用
- POJ 1423 计算阶乘的位数
- iOS随机生成验证码
- day62(5.12)java程序报错类型之类型不匹配
- ubuntu recovery mode 模式root下读写权限
- poj1442 Black Box treap
- JS中数组Array的用法{转载}
- Android图片处理(Matrix,ColorMatrix)
- ubuntu tftp服务的配置
- POSIX 线程详解(3-互斥量:"固定加锁层次"/“试加锁-回退”)
- 创建高性能的android虚拟机,提升安卓虚拟机性能