@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;
#import "XHCodeResignViewh"
#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];
selfbackgroundColor = [UIColor whiteColor];
selfcodeBits= codeBits;
if(self) {
//验证码默认是4位
if(selfcodeBits<1) {
selfcodeBits=4;
}
WEAKSELF
[selfaddSubview:selfcontentF];
[selfbeginEdit];
[selfcontentF mas_makeConstraints:^(MASConstraintMaker make) {
maketopbottomrightleftmas_equalTo(weakSelf)mas_offset(00f);
}];
for(NSIntegeri =0; i
KLCodeViewcodeView =selfcodeViewsArr[i];
[selfaddSubview:codeView];
}
}
return self;
}
- (void)layoutSubviews {
[super layoutSubviews];
WEAKSELF
//设定每个数字之间的间距为数字view宽度的一半 总宽度就是 bits + (bits - 1) 05
CGFloatcodeViewWidth =selfboundssizewidth/(selfcodeBits15-05);
for(NSIntegeri =0; i
KLCodeViewcodeView =selfcodeViewsArr[i];
[codeViewmas_updateConstraints:^(MASConstraintMakermake) {
CGFloatleft = codeViewWidth 15 i;
makeleftmas_equalTo(weakSelf)mas_offset(left);
maketopbottommas_equalTo(weakSelf)mas_offset(00f);
makewidthmas_equalTo(codeViewWidth);
}];
}
}
#pragma mark --- UITextField delegate
- (BOOL)textField:(UITextField )textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString )string {
//完成 则收回键盘
if([stringisEqualToString:@"\n"]) {
[textFieldresignFirstResponder];
returnNO;
}
//删除 *** 作
if([stringisEqualToString:@""])
{
if (selfcurrIndex == 0) {//待输入的下标为0时 删除时下标不变化,否则下标减1
selfcodeViewsArr[selfcurrIndex]text= string;
}else{
selfcodeViewsArr[--selfcurrIndex]text= string;
if (selfcodeResignUnCompleted) {
NSStringcontent = [textFieldtextsubstringToIndex:selfcurrIndex];
selfcodeResignUnCompleted(content);
}
}
returnYES;
}
//判断 输入的是否是纯数字,不是纯数字 输入无效
if(![selfjudgePureInt:string]) {
returnNO;
}
//如果输入的内容超过了验证码的长度 则输入无效
if((textFieldtextlength+ stringlength) >selfcodeBits) {
returnNO;
}
//输入的数字,则当前待输入的下标对应的 view中添加输入的数字,并且下标加1
selfcodeViewsArr[selfcurrIndex++]text = string;
//当当前待输入的下标为codebits时表示已经输入了对应位数的验证码,执行完成 *** 作
if (selfcurrIndex == selfcodeBits && selfcodeResignCompleted) {
NSStringcontent = [NSStringstringWithFormat:@"%@%@", textFieldtext, string];
selfcodeResignCompleted(content);
}else{
if (selfcodeResignUnCompleted) {
NSStringcontent = [NSStringstringWithFormat:@"%@%@", textFieldtext, string];
selfcodeResignUnCompleted(content);
}
}
return YES;
}
- (UITextField )contentF {
if(!_contentF) {
_contentF= [[UITextFieldalloc]init];
//背景颜色和字体颜色都设置为透明的,这样在界面上就看不到
_contentFbackgroundColor = [UIColor clearColor];
_contentFtextColor = [UIColor clearColor];
_contentFkeyboardType = UIKeyboardTypeNumberPad;//数字键盘
_contentFreturnKeyType = UIReturnKeyDone;//完成
_contentFtintColor = [UIColor clearColor];//设置光标的颜色
_contentFdelegate=self;
if(@available(iOS120, )) {
_contentFtextContentType = UITextContentTypeOneTimeCode;
}
}
return _contentF;
}
//开始编辑
- (void)beginEdit{
[_contentF becomeFirstResponder];
}
//结束编辑
- (void)endEdit{
[_contentF resignFirstResponder];
}
- (NSArray )codeViewsArr{
if (!_codeViewsArr) {
NSMutableArray arr = [NSMutableArray array];
for(NSIntegeri =0; i
KLCodeViewcodeView = [[KLCodeViewalloc]init];
[arraddObject:codeView];
}
_codeViewsArr= [NSArrayarrayWithArray:arr];
}
return _codeViewsArr;
}
//判断一个字符串是都是纯数字
- (BOOL)judgePureInt:(NSString)content {
NSScannerscan = [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
selfbackgroundColor = [UIColor whiteColor];
selfuserInteractionEnabled = NO;
//数字编码 label
_codeLabel= [[UILabelalloc]init];
_codeLabeltextColor = [UIColor colorWithHexStr:@"333333"];
_codeLabelfont = [UIFont systemFontOfSize:20];
_codeLabeltextAlignment = NSTextAlignmentCenter;
[selfaddSubview:_codeLabel];
[_codeLabel mas_makeConstraints:^(MASConstraintMaker make) {
maketopleftrightmas_equalTo(weakSelf)mas_offset(00f);
makebottommas_equalTo(weakSelf)mas_offset(00f);
}];
_lineView= [[UIViewalloc]init];
_lineViewbackgroundColor = [UIColor colorWithHexStr:@"f0f0f0"];
[selfaddSubview:_lineView];
[_lineView mas_makeConstraints:^(MASConstraintMaker make) {
makebottomleftrightmas_equalTo(weakSelf)mas_offset(00f);
makeheightmas_equalTo(10f);
}];
}
return self;
}
- (void)setText:(NSString)text {
if(textlength>0) {
selfcodeLabeltext= [textsubstringToIndex:1];
selflineViewbackgroundColor = [UIColor colorWithHexStr:@"999999"];
}else{
selfcodeLabeltext=@"";
selflineViewbackgroundColor = [UIColor colorWithHexStr:@"f0f0f0"];
}
}
@end
selfcodeView = [[XHCodeResignView alloc] initWithCodeBits:4];
selfcodeViewcodeResignCompleted = ^(NSString content) {
wsCodeStr= content;
};
[lineViewaddSubview:selfcodeView];
[selfcodeView mas_makeConstraints:^(MASConstraintMaker make) {
maketopmas_equalTo(0);
makeleadingmas_equalTo(FLoatChange(15));
maketrailingmas_equalTo(FLoatChange(-15));
makeheightmas_equalTo(FLoatChange(54));
}];
参考链接:>
为了防止其他初学者跟我一下,一边学一边谷歌,百度的同学们,少走弯路。对于限制输入个数,这个方法是网上暂时没有的思想,自己想出来。之前2天前写过,单误 *** 作给删除了。如有帮助请仔细看看,自认为挺好的。。。。。
比较:UITextView以及UITextField的限制方法都是一样的。不一样的是
UITextView的代理方法中直接有:
然而UITextField没有,但是UITextField的代理方法最下面有这样一段:
当时,我看到这个感觉跟上面的差不多,都是模式是TextDidChange。
查了一下Notification的意思,通知,所及简而言之:
[NSNotificationCenter defaultCenter]中添加对于UITextField的通知事件
所以,这两个控件都是相同的,我就拿UITextView举例。开始步入正轨。。。。。
分析:
函数的返回值 YES 以及 NO;YES是允许修改textview的值, NO是不允许修改textview的值
我们会用到这两个函数,知道大概的什么时候会调用,那么就来分析一下都有什么情况。
按照原始的认知:英文占一个字符,中文占两个字符。
情况一:
1)全部英文 2)全部中文 3)英文以及中文的结合
情况二:
因为刚接触ios也就有认识来一样东西,叫做表情,类似
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)