.h
@interface XHCodeResignView : UIView
@property (copy, nonatomic) void(^codeResignCompleted)(NSString *content);
@property (copy, nonatomic) void(^codeResignUnCompleted)(NSString *content);
- (instancetype)initWithCodeBits:(NSInteger)codeBits;
//开始编辑
- (void)beginEdit;
//结束编辑
- (void)endEdit;
.m
#import "XHCodeResignView.h"
#import
#define WEAKSELF typeof(self) __weak weakSelf = self;
//自定义 验证码展示视图 view,由一个label和一个下划线组成
@interface KLCodeView : UIView
@property (strong, nonatomic) NSString *text;
@property (strong, nonatomic) UILabel *codeLabel;
@property (strong, nonatomic) UIView *lineView;
@end
@interface XHCodeResignView () <UITextFieldDelegate>
@property (strong, nonatomic) UITextField *contentF; //监听内容输入
@property (strong, nonatomic) NSArray<KLCodeView *> *codeViewsArr;//显示输入内容的codeView数组
@property (assign, nonatomic) NSInteger currIndex;//当前待输入的codeView的下标
@property (assign, nonatomic) NSInteger codeBits;//位数
@end
@implementation XHCodeResignView
- (instancetype)initWithCodeBits:(NSInteger)codeBits
{
self= [superinit];
self.backgroundColor = [UIColor whiteColor];
self.codeBits= codeBits;
if(self) {
//验证码默认是4位
if(self.codeBits<1) {
self.codeBits=4;
}
WEAKSELF
[selfaddSubview:self.contentF];
[selfbeginEdit];
[self.contentF mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.bottom.right.left.mas_equalTo(weakSelf).mas_offset(0.0f);
}];
for(NSIntegeri =0; i
KLCodeView*codeView =self.codeViewsArr[i];
[selfaddSubview:codeView];
}
}
return self;
}
- (void)layoutSubviews {
[super layoutSubviews];
WEAKSELF
//设定每个数字之间的间距为数字view宽度的一半 总宽度就是 bits + (bits - 1)* 0.5
CGFloatcodeViewWidth =self.bounds.size.width/(self.codeBits*1.5-0.5);
for(NSIntegeri =0; i
KLCodeView*codeView =self.codeViewsArr[i];
[codeViewmas_updateConstraints:^(MASConstraintMaker*make) {
CGFloatleft = codeViewWidth *1.5* i;
make.left.mas_equalTo(weakSelf).mas_offset(left);
make.top.bottom.mas_equalTo(weakSelf).mas_offset(0.0f);
make.width.mas_equalTo(codeViewWidth);
}];
}
}
#pragma mark --- UITextField delegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
//完成 则收回键盘
if([stringisEqualToString:@"\n"]) {
[textFieldresignFirstResponder];
returnNO;
}
//删除 操作
if([stringisEqualToString:@""])
{
if (self.currIndex == 0) {//待输入的下标为0时 删除时下标不变化,否则下标减1
self.codeViewsArr[self.currIndex].text= string;
}else{
self.codeViewsArr[--self.currIndex].text= string;
if (self.codeResignUnCompleted) {
NSString*content = [textField.textsubstringToIndex:self.currIndex];
self.codeResignUnCompleted(content);
}
}
returnYES;
}
//判断 输入的是否是纯数字,不是纯数字 输入无效
if(![selfjudgePureInt:string]) {
returnNO;
}
//如果输入的内容超过了验证码的长度 则输入无效
if((textField.text.length+ string.length) >self.codeBits) {
returnNO;
}
//输入的数字,则当前待输入的下标对应的 view中添加输入的数字,并且下标加1
self.codeViewsArr[self.currIndex++].text = string;
//当当前待输入的下标为codebits时表示已经输入了对应位数的验证码,执行完成操作
if (self.currIndex == self.codeBits && self.codeResignCompleted) {
NSString*content = [NSStringstringWithFormat:@"%@%@", textField.text, string];
self.codeResignCompleted(content);
}else{
if (self.codeResignUnCompleted) {
NSString*content = [NSStringstringWithFormat:@"%@%@", textField.text, string];
self.codeResignUnCompleted(content);
}
}
return YES;
}
- (UITextField *)contentF {
if(!_contentF) {
_contentF= [[UITextFieldalloc]init];
//背景颜色和字体颜色都设置为透明的,这样在界面上就看不到
_contentF.backgroundColor = [UIColor clearColor];
_contentF.textColor = [UIColor clearColor];
_contentF.keyboardType = UIKeyboardTypeNumberPad;//数字键盘
_contentF.returnKeyType = UIReturnKeyDone;//完成
_contentF.tintColor = [UIColor clearColor];//设置光标的颜色
_contentF.delegate=self;
if(@available(iOS12.0, *)) {
_contentF.textContentType = UITextContentTypeOneTimeCode;
}
}
return _contentF;
}
//开始编辑
- (void)beginEdit{
[_contentF becomeFirstResponder];
}
//结束编辑
- (void)endEdit{
[_contentF resignFirstResponder];
}
- (NSArray *)codeViewsArr{
if (!_codeViewsArr) {
NSMutableArray *arr = [NSMutableArray array];
for(NSIntegeri =0; i
KLCodeView*codeView = [[KLCodeViewalloc]init];
[arraddObject:codeView];
}
_codeViewsArr= [NSArrayarrayWithArray:arr];
}
return _codeViewsArr;
}
//判断一个字符串是都是纯数字
- (BOOL)judgePureInt:(NSString*)content {
NSScanner*scan = [NSScannerscannerWithString:content];
intval;
return[scanscanInt:&val] && [scanisAtEnd];
}
@end
@implementationUITextField(ForbiddenSelect)
/*
该函数控制是否允许 选择 全选 剪切 f粘贴等功能,可以针对不同功能进行限制
返回YES表示允许对应的功能,返回NO则表示不允许对应的功能
直接返回NO则表示不允许任何编辑
*/
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
return NO;
}
@end
//验证码展示视图 的实现
@implementation KLCodeView
- (instancetype)initWithFrame:(CGRect)frame {
self= [superinitWithFrame:frame];
if(self) {
WEAKSELF
self.backgroundColor = [UIColor whiteColor];
self.userInteractionEnabled = NO;
//数字编码 label
_codeLabel= [[UILabelalloc]init];
_codeLabel.textColor = [UIColor colorWithHexStr:@"333333"];
_codeLabel.font = [UIFont systemFontOfSize:20];
_codeLabel.textAlignment = NSTextAlignmentCenter;
[selfaddSubview:_codeLabel];
[_codeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.left.right.mas_equalTo(weakSelf).mas_offset(0.0f);
make.bottom.mas_equalTo(weakSelf).mas_offset(0.0f);
}];
_lineView= [[UIViewalloc]init];
_lineView.backgroundColor = [UIColor colorWithHexStr:@"f0f0f0"];
[selfaddSubview:_lineView];
[_lineView mas_makeConstraints:^(MASConstraintMaker *make) {
make.bottom.left.right.mas_equalTo(weakSelf).mas_offset(0.0f);
make.height.mas_equalTo(1.0f);
}];
}
return self;
}
- (void)setText:(NSString*)text {
if(text.length>0) {
self.codeLabel.text= [textsubstringToIndex:1];
self.lineView.backgroundColor = [UIColor colorWithHexStr:@"999999"];
}else{
self.codeLabel.text=@"";
self.lineView.backgroundColor = [UIColor colorWithHexStr:@"f0f0f0"];
}
}
@end
VC引用:
self.codeView = [[XHCodeResignView alloc] initWithCodeBits:4];
self.codeView.codeResignCompleted = ^(NSString *content) {
ws.CodeStr= content;
};
[lineViewaddSubview:self.codeView];
[self.codeView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(0);
make.leading.mas_equalTo(FLoatChange(15));
make.trailing.mas_equalTo(FLoatChange(-15));
make.height.mas_equalTo(FLoatChange(54));
}];
参考链接:https://www.cnblogs.com/mukekeheart/p/10142188.html