iOS 随机验证码

最近在项目中用到了随机验证码,在这里简单做个记录。
效果图:在这里插入图片描述

主要代码:

.h文件
#import <UIKit/UIKit.h>

typedef NS_ENUM(NSInteger,XBGetVerificationCodeType)

{
    
    XBGetVerificationCodeOne = 0,//只数字
    
    XBGetVerificationCodeTwo,//只字母
    
    XBGetVerificationCodeThree,//数字字母混合
};

typedef void(^setShowCodeNumber)(NSString * codeNumber);

@interface XBGetVerificationCodeView : UIView

@property(nonatomic,assign)NSInteger verificationNumber;//验证码位数

@property(nonatomic,assign)XBGetVerificationCodeType tmpCodeType;

@property(nonatomic,strong)setShowCodeNumber setShowCodeNumber;//获取到的验证码

-(void)setCodeTitleColor:(UIColor *)color;// 设置验证码字体颜色

-(void)setCodeTitleFont:(UIFont *)font;//设置验证码字体大小

@end
.m 文件
#import "XBGetVerificationCodeView.h"

@interface XBGetVerificationCodeView()

@property(nonatomic,strong)UIButton * btn;

@end

@implementation XBGetVerificationCodeView

-(id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    
    if(self)
    {
        self.verificationNumber = 6;
        
        self.btn = [UIButton buttonWithType:UIButtonTypeCustom];
        
        [self.btn setFrame:CGRectMake(0, 0, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame))];
        
        [self.btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        
        [self.btn setTitle:@"获取验证码" forState:UIControlStateNormal];
        
        [self.btn.titleLabel setFont:[UIFont systemFontOfSize:14.0]];
        
        [self.btn addTarget:self action:@selector(setBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        
        [self addSubview:self.btn];
    }
    
    return self;
}

-(void)setBtnAction:(UIButton *)sender
{
    if(sender && [sender isKindOfClass:[UIButton class]])
    {
        [self.btn setTitle:[NSString stringWithFormat:@"%@",[self getRandomStringWithNum:self.verificationNumber]] forState:UIControlStateNormal];
        
        if(self.setShowCodeNumber)
        {
            self.setShowCodeNumber([NSString stringWithFormat:@"%@",[self getRandomStringWithNum:self.verificationNumber]]);
        }
        
    }
}

-(void)setCodeTitleColor:(UIColor *)color
{
    [self.btn setTitleColor:color forState:UIControlStateNormal];
}
-(void)setCodeTitleFont:(UIFont *)font
{
    [self.btn.titleLabel setFont:font];
}
-(NSString *)getRandomStringWithNum:(NSInteger)num
{
    
    NSString *string = [[NSString alloc]init];
    
    if(self.tmpCodeType == XBGetVerificationCodeOne)
    {
        for (int i = 0; i < num; i++) {
           
            int figure = arc4random() % 10;
           
            NSString *tempString = [NSString stringWithFormat:@"%d", figure];
           
            string = [string stringByAppendingString:tempString];
           
        }
        return string;
    }
    else if(self.tmpCodeType == XBGetVerificationCodeTwo)
    {
        for (int i = 0; i < num; i++) {
            
            int figure = (arc4random() % 26) + 97;
            
            char character = figure;
           
            NSString *tempString = [NSString stringWithFormat:@"%c", character];
           
            string = [string stringByAppendingString:tempString];
            
        }
        return string;
    }
    else
    {
        for (int i = 0; i < num; i++) {
            int number = arc4random() % 36;
            if (number < 10) {
                int figure = arc4random() % 10;
                NSString *tempString = [NSString stringWithFormat:@"%d", figure];
                string = [string stringByAppendingString:tempString];
            }else {
                int figure = (arc4random() % 26) + 97;
                char character = figure;
                NSString *tempString = [NSString stringWithFormat:@"%c", character];
                string = [string stringByAppendingString:tempString];
            }
        }
        return string;
    }
    
    
}
@end

使用

XBGetVerificationCodeView * xbView = [[XBGetVerificationCodeView alloc]initWithFrame:CGRectMake(100, 100, 100, 40)];
   
    [xbView setBackgroundColor:[UIColor orangeColor]];
    
    xbView.tmpCodeType = XBGetVerificationCodeThree;
    
    [xbView setSetShowCodeNumber:^(NSString *codeNumber) {
       
        NSLog(@"验证码为:%@",codeNumber);
    }];
    
    [self.view addSubview:xbView];

demo地址:https://gitee.com/xiaobaidxg/VerificationCode.git

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值