Untitled.gif
前言:
需求如上,需要設(shè)計輸入密碼的方格類型,本人進(jìn)行了簡單的封裝,可以設(shè)置方格的個數(shù),方格邊框的顏色,支持密碼刪除,下面給出分裝的詳細(xì)代碼以及使用方法
思路:
自定義一個UIView然后在添加一個UITextField這樣可以保證點擊方格以外的部分可以鍵盤的編輯狀態(tài)(要把這個UITextField隱藏才行),首先創(chuàng)建幾個輸入框,用個數(shù)組保存,然后把鍵盤輸入的內(nèi)容,通過遍歷數(shù)組內(nèi)的輸入方格,將輸入的內(nèi)容按順序賦值給這些方格
具體代碼,在.h文件中利用block回調(diào)輸入的密碼
#import <UIKit/UIKit.h>
typedef void (^ReturnPasswordStringBlock)(NSString *password);
@interface PasswordView : UIView
@property (copy, nonatomic) ReturnPasswordStringBlock returnPasswordStringBlock;
@end
在.m文件中具體實現(xiàn)
- 可以在此文件中自定義密碼的位數(shù),方格邊框的顏色,方格的尺寸
#import "PasswordView.h"
// 輸入密碼的位數(shù)
static const int boxCount = 6;
// 輸入方格的邊長
static const CGFloat boxWH = 40;
@interface PasswordView()
@property (strong, nonatomic) NSMutableArray *boxes;
// 占位編輯框(這樣就點擊密碼格以外的部分,可以彈出鍵盤)
@property (weak, nonatomic) UITextField *contentTextField;
@end
@implementation PasswordView
- (instancetype)initWithFrame:(CGRect)frame{
if (self == [super initWithFrame:frame]) {
self.backgroundColor = [UIColor whiteColor];
[self setUpContentView];
}
return self;
}
- (void)setUpContentView
{
UITextField *contentField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
self.contentTextField = contentField;
contentField.placeholder = @"請輸入支付密碼";
contentField.hidden = YES;
[contentField addTarget:self action:@selector(txchange:) forControlEvents:UIControlEventEditingChanged];
[self addSubview:contentField];
// 密碼格之間的間隔
CGFloat margin = 10;
for (int i = 0; i < boxCount; i++)
{
CGFloat x = ([UIScreen mainScreen].bounds.size.width - boxCount * boxWH - (boxCount - 1)* margin) * 0.5 + (boxWH + margin) * i;
UITextField *pwdLabel = [[UITextField alloc] initWithFrame:CGRectMake(x, (self.frame.size.height - boxWH) * 0.5, boxWH, boxWH)];
pwdLabel.layer.borderColor = LineCommanColorA.CGColor;
pwdLabel.enabled = NO;
pwdLabel.textAlignment = NSTextAlignmentCenter;//居中
pwdLabel.secureTextEntry = YES;//設(shè)置密碼模式
pwdLabel.layer.borderWidth = 1;
[self addSubview:pwdLabel];
[self.boxes addObject:pwdLabel];
}
//進(jìn)入界面黔寇,contentTextField 成為第一響應(yīng)
[self.contentTextField becomeFirstResponder];
}
#pragma mark 文本框內(nèi)容改變
- (void)txchange:(UITextField *)tx
{
NSString *password = tx.text;
for (int i = 0; i < self.boxes.count; i++)
{
UITextField *pwdtx = [self.boxes objectAtIndex:i];
pwdtx.text = @"";
if (i < password.length)
{
NSString *pwd = [password substringWithRange:NSMakeRange(i, 1)];
pwdtx.text = pwd;
}
}
// 輸入密碼完畢
if (password.length == boxCount)
{
[tx resignFirstResponder];//隱藏鍵盤
if (self.returnPasswordStringBlock != nil) {
self.returnPasswordStringBlock(password);
}
}
}
#pragma mark --- 懶加載
- (NSMutableArray *)boxes{
if (!_boxes) {
_boxes = [NSMutableArray array];
}
return _boxes;
}
@end
分類的用法:
首先是導(dǎo)入頭文件
PasswordView *pdView = [[PasswordView alloc] initWithFrame:CGRectMake(0, 200, self.view.bounds.size.width, 60)];
pdView.returnPasswordStringBlock = ^(NSString *password){
// 這里可以對輸入的密碼進(jìn)行處理
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"輸入的密碼是" message:password delegate:nil cancelButtonTitle:@"完成" otherButtonTitles:nil, nil];
[alert show];
};
[self.view addSubview:pdView];