//
//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