我们经常会在手机应用中会看到输入验证码,然后操作一个东西,那么我们应该如何去实现他呢,先看一下效果 verificationCode.gif 1、首先我们定义一个继承 UIView 的类 ZYCaptchaView 用于、生成验证码的操作 2、 .h 文件中我们这样声明 /**字符数组 */ @property (nonatomic , strong)NSArray *characterArray; /** 验证码字符串 */ @property (nonatomic , strong)NSMutableString *captchaString; /** 加载验证码 */ - (void)loadCaptcha; 3、 .m 文件中我们具体去实现 首先,我们在 initWithFrame 中生成一个随机的验证码 - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { //设置layer圆角半径 self.layer.cornerRadius = 5.0; //影藏边境 self.layer.masksToBounds = YES; self.backgroundColor = kRandomColor; //显示一个随机验证码 [self randomCaptcha]; } return self; } #pragma mark -- 更换验证码 - (void)randomCaptcha { //数组中存放的是全部可选的字符,可以是字母,也可以是中文 self.characterArray = [[NSArray alloc]initWithObjects:@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J",@"K",@"L",@"M",@"N",@"O",@"P",@"Q",@"R",@"S",@"T",@"U",@"V",@"W",@"X",@"Y",@"Z",@"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",@"i",@"j",@"k",@"l",@"m",@"n",@"o",@"p",@"q",@"r",@"s",@"t",@"u",@"v",@"w",@"x",@"y",@"z", nil]; NSMutableString *getStr = [[NSMutableString alloc] initWithCapacity:kCharCount]; self.captchaString = [[NSMutableString alloc] initWithCapacity:kCharCount]; //随机从数组中选取需要个数的字符,然后拼接为一个字符串 for (int i = 0 ; i < kCharCount; i ++) { int index = arc4random() % (self.characterArray.count - 1); getStr = [self.characterArray objectAtIndex:index]; self.captchaString = [[self.captchaString stringByAppendingString:getStr] copy]; } //从网络获取字符串,然后把得到的字符串在本地绘制出来(网络获取步骤在这省略) } 其次,我们实现加载验证码的代码 /** 加载验证码 */ - (void)loadCaptcha { //更换验证码 [self randomCaptcha]; //重新绘制 [self setNeedsDisplay]; } 最后我们实现绘制验证码的代码,也就是重写 drawRect 方法 #pragma mark 绘制界面 - (void)drawRect:(CGRect)rect { [super drawRect:rect]; //设置随机背景颜色 self.backgroundColor = kRandomColor; //获取要显示的验证码字符串 NSString *text = [NSString stringWithFormat:@"%@" , self.captchaString]; CGSize cSize = [@"s" sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:20]}]; int width = rect.size.width / text.length - cSize.width; int height = rect.size.height - cSize.height; CGPoint point; //依次绘制每一个字符,可以设置显示的每个字符的字体大小、颜色、样式等 float pX , pY; for (int i = 0; i < text.length; i ++) { pX = arc4random() % width + rect.size.width / text.length * i; pY = arc4random() % height; point = CGPointMake(pX, pY); unichar c = [text characterAtIndex:i]; NSString *textC = [NSString stringWithFormat:@"%c" , c]; [textC drawAtPoint:point withAttributes:@{NSFontAttributeName:kFontSize}]; } //获取上下文 CGContextRef context = UIGraphicsGetCurrentContext(); //设置画线宽度 CGContextSetLineWidth(context, kLineWidth); //绘制干扰的彩色直线 for (int i = 0; i < kLineCount; i ++) { //设置线的随机颜色 UIColor *color = kRandomColor; CGContextSetStrokeColorWithColor(context, color.CGColor); //设置线的起点 pX = arc4random() % (int)rect.size.width; pY = arc4random() % (int)rect.size.height; CGContextMoveToPoint(context, pX, pY); //设置线的终点 pX = arc4random() % (int)rect.size.width; pY = arc4random() % (int)rect.size.height; CGContextAddLineToPoint(context, pX, pY); //画线 CGContextStrokePath(context); } } 4、在控制器中调用我们的类 需要注意的是我们需要让验证码不区分大小写,我们就需要大小写进行转换,你可以将所有字母转换为大写,也可以转换为小写。大小写转换的代码如下: /** 大写字母转换为小写 */ - (NSString *)toLower:(NSString *)str { for (int i = 0; i < str.length; i++) { if ([str characterAtIndex:i] >= 'A' && [str characterAtIndex:i] <= 'Z') { char temp = [str characterAtIndex:i] + 32; NSRange range = NSMakeRange(i, 1); str = [str stringByReplacingCharactersInRange:range withString:[NSString stringWithFormat:@"%c" , temp]]; } } return str; } /** 小写字母转换为大写 */ -(NSString *)toUpper:(NSString *)str{ for (int i = 0; i < str.length; i++) { if ([str characterAtIndex:i]>='a'&[str characterAtIndex:i]<='z') { char temp=[str characterAtIndex:i]-32; NSRange range=NSMakeRange(i, 1); str=[str stringByReplacingCharactersInRange:range withString:[NSString stringWithFormat:@"%c",temp]]; } } return str; } 大概就这些步骤了,如有需 (责任编辑:最模板) |