体验非常好的密码输入框

       最近公司做商城的项目,期间用到一个类似支付宝输入支付密码的效果输入框,看了下网络上不少小伙伴小的Demo,都不是很满意,支付宝的体验效果与原声的密码输入效果也存在不少差距,于是自己想了个实现方法,记录在。


需求分析:



需求分析

效果图:



具体实现:

1.新建一个类:CPPasswdView继承至UILabel,支持:CPPasswdView协议;

2.声明 “CPPasswdView”、“inputAccessoryView"两个属性为可读写;

3.声明密码长度(默认为6);

#import @interface CPPasswdView : UILabel

@property (nonatomic, strong) UIView *inputView;

@property (nonatomic, strong) UIView *inputAccessoryView;

@property (nonatomic, assign) NSInteger passwdLength;

@end

4.在CPPasswdView.m 中实现UIKeyInput的几个协议;

- (BOOL)hasText;

- (void)insertText:(NSString *)text;

- (void)deleteBackward;

5.设置视图为可输入模式

- (BOOL)canBecomeFirstResponder;    // default is NO

6.设置CPPasswdView视图为可交互;

self.userInteractionEnabled = YES;  //  一定要将该属性设置成可YES,否则软键盘出不来

7.将试图分割成指定的块;

//  绘制分割线

- (void)setupUpUI {         

            CGFloat width  = self.bounds.size.width / self.passwdLength;    

            CGFloat height = self.bounds.size.height;         

            for (NSInteger i = 1; i < self.passwdLength; ++i) {               

                      UIView *line = [[UIView alloc] initWithFrame:CGRectMake(i * width, 0, 1, height)];                                line.backgroundColor = UIColor.grayColor;               

                      [self addSubview:line];             

            }

}

8.处理输入的字符串

#pragma mark - Private method

- (void)updateContent {         

        {        

                //  替换除最后一个字符外的其他字符为*  eg: **********8        

                NSMutableString *tempstr = @"".mutableCopy;        

                for (NSInteger i = 0; i < self.secureStr.length - 1 && self.secureStr.length > 0; ++i) {            

                [tempstr appendString:@"*"];        

                }        

                if (self.secureStr.length > 0) {            

                [self.secureStr replaceCharactersInRange:NSMakeRange(0, self.secureStr.length - 1) withString:tempstr];        

                }                 

                self.attributedText = [[NSAttributedString alloc] initWithString:self.secureStr attributes:self.options];    

        }              

        {        

        //  在一秒后设置最后一个字符为"*"        

                if (self.secureStr.length > 0) {            

                            [NSObject cancelPreviousPerformRequestsWithTarget:self  selector:@selector(showWithSecurText:) object:nil];          

                          [self performSelector:@selector(showWithSecurText:) withObject:nil afterDelay:1.0f];        

                }    

        }     

}

//  将最后一个字符串替换为"*"

- (void)showWithSecurText:(id)object {         

        if (self.secureStr.length > 0) {        

        NSInteger location = MAX(0, (NSInteger)(self.secureStr.length) - 1);        

        [self.secureStr replaceCharactersInRange:NSMakeRange(location, 1) withString:@"*"];     }         

        self.attributedText = [[NSAttributedString alloc] initWithString:self.secureStr attributes:self.options];

}

9.CPPasswdView.m 代码实现Demo地址

你可能感兴趣的:(体验非常好的密码输入框)