IOS 验证码
来源:互联网 发布:网站美工设计培训 编辑:程序博客网 时间:2024/06/06 11:44
<span style="font-size:18px;">- (void)onTapToGenerateCode:(UITapGestureRecognizer *)tap { for (UIView *view in labelCodeNum.subviews) { [view removeFromSuperview]; } //生成背景颜色 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]; labelCodeNum.backgroundColor = color; //生成文字 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]; CGSize cSize = [@"S" sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17.0], NSForegroundColorAttributeName:[UIColor grayColor]}]; NSInteger width = labelCodeNum.frame.size.width / text.length - cSize.width; NSInteger height = labelCodeNum.frame.size.height - cSize.height; CGPoint point; float pX, pY; for ( int i = 0, count = (int)text.length; i < count; i++) { pX = arc4random() % width + labelCodeNum.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, labelCodeNum.frame.size.width/4, labelCodeNum.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; [labelCodeNum 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)labelCodeNum.frame.size.width; pY = arc4random() % (int)labelCodeNum.frame.size.height; CGContextMoveToPoint(context, pX, pY); pX = arc4random() % (int)labelCodeNum.frame.size.width; pY = arc4random() % (int)labelCodeNum.frame.size.height; CGContextAddLineToPoint(context, pX, pY); CGContextStrokePath(context); } return;}</span>
效果图:
0 0
- iOS验证码
- IOS 验证码
- iOS 短信验证码
- ios 验证码
- ios 验证码倒计时
- ios 验证码倒计时
- ios 验证码倒计时
- ios 验证码生成
- iOS短信验证码
- iOS--本地验证码
- iOS 图片验证码
- iOS开发验证码
- iOS 验证码倒计时
- [IOS]SMS获取&验证手机验证码
- iOS随机生成验证码
- iOS随机生成验证码
- iOS 计时器-验证码相关
- IOS 生成本地验证码
- 图片懒加载
- 推荐系统之实践篇
- 第一周项目 旱冰场造价
- 在cocos2d-x中加入一个UIView
- SQL语言常用
- IOS 验证码
- UITableViewCell的基本使用
- Why is celsius = 5 * (fahr - 32) / 9 ?
- ios ----UIScrollview循环滑动
- eclipse启动报错解决
- 编写一个函数,传入a,b两个int类型的变量,返回两个值的最大公约数
- Excel只能输入不能修改
- Android Tween动画之RotateAnimation实现图片不停旋转
- spring的IOC和AOP