自定义多位密码框

//

//LZTextField.m

//TEstTextFiled

//

//Created by k on 2017/3/30.

//Copyright © 2017年king. All rights reserved.

//

#import"LZTextField.h"

@interfaceLZTextField()

/**

线条集合

*/

@property(nonatomic,strong)NSMutableArray*hrImgeMary;

/**

密码圆点集合

*/

@property(nonatomic,strong)NSMutableArray*pwdMary;

@end

@implementationLZTextField

- (id)initWithFrame:(CGRect)frame

{

self= [superinitWithFrame: frame];

if(self)

{

self.backgroundColor= [UIColorwhiteColor];

self.layer.borderColor= [UIColorredColor].CGColor;

self.layer.cornerRadius=8;

self.layer.borderWidth=.5;

self.clipsToBounds=YES;

UITextField*pwTextField = [[UITextFieldalloc]init];

pwTextField.textColor= [UIColorclearColor];

//光标的颜色值

pwTextField.tintColor= [UIColorwhiteColor];

pwTextField.keyboardType=UIKeyboardTypeNumberPad;

//定义文本自动大小写样式。UITextAutocapitalizationTypeNone关闭自动大写

pwTextField.autocapitalizationType=UITextAutocapitalizationTypeNone;

pwTextField.textColor=self.backgroundColor;

pwTextField.layer.borderWidth=0.5;

pwTextField.alpha=0.1;

[pwTextFieldaddTarget:selfaction:@selector(textFieldDidChange:)forControlEvents:UIControlEventEditingChanged];

pwTextField.delegate=self;

[selfaddSubview:pwTextField];

self.pwTextField= pwTextField;

}

returnself;

}

- (NSMutableArray*)hrImgeMary

{

if(!_hrImgeMary)

{

NSMutableArray*hrImgeMary = [NSMutableArrayarrayWithCapacity:0];

_hrImgeMary= hrImgeMary;

}

return_hrImgeMary;

}

- (NSMutableArray*)pwdMary

{

if(!_pwdMary)

{

NSMutableArray*pwdMary = [NSMutableArrayarrayWithCapacity:0];

_pwdMary= pwdMary;

}

return_pwdMary;

}

- (void)setPwdCountInt:(NSInteger)pwdCountInt

{

_pwdCountInt= pwdCountInt;

CGFloathrImgViewWidth =CGRectGetWidth(self.bounds) / pwdCountInt;

CGFloatsubViewSHeigth =CGRectGetHeight(self.bounds);

self.pwTextField.frame=CGRectMake(0,0,CGRectGetWidth(self.bounds), subViewSHeigth);

//分割线

for(inti =0; i < pwdCountInt -1; i++)

{

UIImageView*hrImgView = [[UIImageViewalloc]initWithFrame:CGRectMake((i +1)*hrImgViewWidth,0,1, subViewSHeigth)];

[self.hrImgeMaryaddObject:hrImgView];

[selfaddSubview:hrImgView];

}

//密码点

for(inti =0; i < pwdCountInt; i++) {

UIView*pwdViw = [[UIViewalloc]initWithFrame:CGRectMake(i * hrImgViewWidth + hrImgViewWidth /2,

(subViewSHeigth -self.pwdWidthOrHeight) /2,

self.pwdWidthOrHeight,

self.pwdWidthOrHeight)];

pwdViw.hidden=YES;

pwdViw.backgroundColor= [UIColorredColor];

pwdViw.layer.cornerRadius=_pwdWidthOrHeight/2;

pwdViw.backgroundColor=self.pwdTextColor;

pwdViw.clipsToBounds=YES;

[self.pwdMaryaddObject:pwdViw];

[selfaddSubview:pwdViw];

}

}

- (void)setTextFieldLayerColor:(UIColor*)textFieldLayerColor

{

self.layer.borderColor= textFieldLayerColor.CGColor;

}

- (void)setPwdWidthOrHeight:(CGFloat)pwdWidthOrHeight

{

_pwdWidthOrHeight= pwdWidthOrHeight;

}

- (void)textFieldDidChange:(UITextField*)uiTextField

{

for(UIView*objectViewinself.pwdMary)

{

if([objectViewisKindOfClass:[UIViewclass]])

{

objectView.hidden=YES;

}

}

for(inti =0; i < uiTextField.text.length; i++)

{

UIView*pwdView = (UIView*)[self.pwdMaryobjectAtIndex:i];

pwdView.hidden=NO;

}

}

- (void)setHrImgColor:(UIColor*)hrImgColor

{

for(UIView*hrimgeViewinself.hrImgeMary) {

if([hrimgeViewisKindOfClass:[UIImageViewclass]])hrimgeView.backgroundColor= hrImgColor;

}

}

- (void)setPwdTextColor:(UIColor*)pwdTextColor

{

for(UIView*objectViewinself.pwdMary)

{

objectView.backgroundColor= pwdTextColor;

}

}

- (BOOL)textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString*)string

{

//屏蔽表情输入

if([[[UITextInputModecurrentInputMode]primaryLanguage]isEqualToString:@"emoji"] && string.length)

{

returnNO;

}

if(string.length==0) {

//判断是不是删除键

returnYES;

}

if(textField.text.length>=self.pwdCountInt) {

returnNO;

}

returnYES;

}

@end

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥iOS动画全貌。在这里你可以看...
    F麦子阅读 10,536评论 5 13
  • 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥ios动画全貌。在这里你可以看...
    每天刷两次牙阅读 12,720评论 6 30
  • 1、设置UILabel行间距 NSMutableAttributedString*attrString=[[NSM...
    iOS祎阅读 6,628评论 0 0
  • //设置尺寸为屏幕尺寸的时候self.window = [[UIWindow alloc] initWithFra...
    LuckTime阅读 4,298评论 0 0
  • 1、设置UILabel行间距 NSMutableAttributedString* attrString = [[...
    十年一品温如言1008阅读 5,870评论 0 3

友情链接更多精彩内容