IOS實(shí)現(xiàn)輸入驗(yàn)證碼、密碼按位分割(二)
本文提供了實(shí)現(xiàn)IOS實(shí)現(xiàn)輸入驗(yàn)證碼、密碼按位分割的一種思路,分享給大家供大家參考,希望與大家共同交流。
一、實(shí)現(xiàn)思路
1、思路描述
- 自定義一個(gè)view,繼承自UIView
- 在view中添加子控件textField,backgroundImageView,label
- 將驗(yàn)證碼/密碼的內(nèi)容繪制到label的指定區(qū)域(計(jì)算得到),所以label要自定義,在drawRect方法中繪制驗(yàn)證碼
- 使用一個(gè)屬性secureTextEntry,來(lái)控制顯示驗(yàn)證碼(顯示真實(shí)的數(shù)字)或密碼(顯示圓點(diǎn))
2、視圖中的子控件
- textField:只負(fù)責(zé)彈出鍵盤(pán),獲取鍵盤(pán)輸入的數(shù)據(jù);不用于演示鍵盤(pán)輸入的內(nèi)容,實(shí)際是隱藏的
- backgroundImageView:顯示實(shí)現(xiàn)分割效果的背景圖片
- label:顯示驗(yàn)證碼或密碼的內(nèi)容
3、控件之間的關(guān)系
如圖:
- 編號(hào)“1”:父視圖(vertificationCodeInputView)
- 編號(hào)“2”:子視圖(textField)
- 編號(hào)“3”:子視圖(backgroundImageView)
- 編號(hào)“4”:子視圖(label)
- 圖片來(lái)源于Xcode的調(diào)試工具
層級(jí)關(guān)系
- label用于顯示驗(yàn)證碼的內(nèi)容,必須在最上邊
- backgroundImageView顯示背景圖片,所以必須在label的后邊,且可以顯示出來(lái)
二、實(shí)現(xiàn)效果
密碼輸入效果
驗(yàn)證碼輸入效果
三、實(shí)現(xiàn)步驟
代碼結(jié)構(gòu)
如圖:
1、IDVertificationCodeInputView(編號(hào)“1”視圖)的的屬性
公有屬性(vertificationCodeInputView的相關(guān)屬性)
@interface IDVertificationCodeInputView : UIView /**背景圖片*/ @property (nonatomic, copy) NSString *backgroudImageName; /**驗(yàn)證碼/密碼的位數(shù)*/ @property (nonatomic, assign) NSInteger numberOfVertificationCode; /**控制驗(yàn)證碼/密碼是否密文顯示*/ @property (nonatomic, assign) bool secureTextEntry; /**驗(yàn)證碼/密碼內(nèi)容,可以通過(guò)該屬性拿到驗(yàn)證碼/密碼輸入框中驗(yàn)證碼/密碼的內(nèi)容*/ @property (nonatomic, copy) NSString *vertificationCode; @end
私有屬性(vertificationCodeInputView的子控件)
@interface IDVertificationCodeInputView () <UITextFieldDelegate> /**用于獲取鍵盤(pán)輸入的內(nèi)容,實(shí)際不顯示*/ @property (nonatomic, strong) UITextField *textField; /**驗(yàn)證碼/密碼輸入框的背景圖片*/ @property (nonatomic, strong) UIImageView *backgroundImageView; /**實(shí)際用于顯示驗(yàn)證碼/密碼的label*/ @property (nonatomic, strong) IDLabel *label; @end
2、IDLabel(編號(hào)“4”視圖)的屬性
公有屬性
@interface IDLabel : UILabel /**驗(yàn)證碼/密碼的位數(shù)*/ @property (nonatomic, assign) NSInteger numberOfVertificationCode; /**控制驗(yàn)證碼/密碼是否密文顯示*/ @property (nonatomic, assign) bool secureTextEntry; @end
3、業(yè)務(wù)邏輯
vertificationCodeInputView的初始化
- 設(shè)置驗(yàn)證碼/密碼的默認(rèn)位數(shù)(4位),添加textField和label
- (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { // 設(shè)置透明背景色,保證vertificationCodeInputView顯示的frame為backgroundImageView的frame self.backgroundColor = [UIColor clearColor]; // 設(shè)置驗(yàn)證碼/密碼的位數(shù)默認(rèn)為四位 self.numberOfVertificationCode = 4; /* 調(diào)出鍵盤(pán)的textField */ self.textField = [[UITextField alloc] initWithFrame:self.bounds]; // 隱藏textField,通過(guò)點(diǎn)擊IDVertificationCodeInputView使其成為第一響應(yīng)者,來(lái)彈出鍵盤(pán) self.textField.hidden = YES; self.textField.keyboardType = UIKeyboardTypeNumberPad; self.textField.delegate = self; // 將textField放到最后邊 [self insertSubview:self.textField atIndex:0]; /* 添加用于顯示驗(yàn)證碼/密碼的label */ self.label = [[IDLabel alloc] initWithFrame:self.bounds]; self.label.numberOfVertificationCode = self.numberOfVertificationCode; self.label.secureTextEntry = self.secureTextEntry; self.label.font = self.textField.font; [self addSubview:self.label]; } return self; }
- 若設(shè)置了背景圖片,需要將backgroundImageView添加進(jìn)vertificationCodeInputView
- (void)setBackgroudImageName:(NSString *)backgroudImageName { _backgroudImageName = backgroudImageName; // 若用戶設(shè)置了背景圖片,則添加背景圖片 self.backgroundImageView = [[UIImageView alloc] initWithFrame:self.bounds]; self.backgroundImageView.image = [UIImage imageNamed:self.backgroudImageName]; // 將背景圖片插入到label的后邊,避免遮擋驗(yàn)證碼/密碼的顯示 [self insertSubview:self.backgroundImageView belowSubview:self.label]; }
- 若設(shè)置了驗(yàn)證碼/密碼的位數(shù),和是否密文顯示,則需要保持label中相應(yīng)屬性與vertificationCodeInputView中一致
- (void)setNumberOfVertificationCode:(NSInteger)numberOfVertificationCode { _numberOfVertificationCode = numberOfVertificationCode; // 保持label的驗(yàn)證碼/密碼位數(shù)與IDVertificationCodeInputView一致,此時(shí)label一定已經(jīng)被創(chuàng)建 self.label.numberOfVertificationCode = _numberOfVertificationCode; } - (void)setSecureTextEntry:(bool)secureTextEntry { _secureTextEntry = secureTextEntry; self.label.secureTextEntry = _secureTextEntry; }
4、彈出鍵盤(pán),并接收鍵盤(pán)輸入的字符
彈出鍵盤(pán)
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { [self.textField becomeFirstResponder]; }
接收鍵盤(pán)輸入的字符
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { // 判斷是不是“刪除”字符 if (string.length != 0) {// 不是“刪除”字符 // 判斷驗(yàn)證碼/密碼的位數(shù)是否達(dá)到預(yù)定的位數(shù) if (textField.text.length < self.numberOfVertificationCode) { self.label.text = [textField.text stringByAppendingString:string]; self.vertificationCode = self.label.text; return YES; } else { return NO; } } else { // 是“刪除”字符 self.label.text = [textField.text substringToIndex:textField.text.length - 1]; self.vertificationCode = self.label.text; return YES; } }
5、繪制驗(yàn)證碼/密碼(IDLabel)
手動(dòng)調(diào)用drawRect方法
//重寫(xiě)setText方法,當(dāng)text改變時(shí)手動(dòng)調(diào)用drawRect方法,將text的內(nèi)容按指定的格式繪制到label上 - (void)setText:(NSString *)text { [super setText:text]; // 手動(dòng)調(diào)用drawRect方法 [self setNeedsDisplay]; }
繪制驗(yàn)證碼/密碼
// 按照指定的格式繪制驗(yàn)證碼/密碼 - (void)drawRect:(CGRect)rect { //計(jì)算每位驗(yàn)證碼/密碼的所在區(qū)域的寬和高 float width = rect.size.width / (float)self.numberOfVertificationCode;; float height = rect.size.height; // 將每位驗(yàn)證碼/密碼繪制到指定區(qū)域 for (int i = 0; i < self.text.length; i++) { // 計(jì)算每位驗(yàn)證碼/密碼的繪制區(qū)域 CGRect tempRect = CGRectMake(i * width, 0, width, height); if (self.secureTextEntry) { // 密碼,顯示圓點(diǎn) UIImage *dotImage = [UIImage imageNamed:@"dot"]; // 計(jì)算圓點(diǎn)的繪制區(qū)域 CGPoint securityDotDrawStartPoint = CGPointMake(width * i + (width - dotImage.size.width) / 2.0, (tempRect.size.height - dotImage.size.height) / 2.0); // 繪制圓點(diǎn) [dotImage drawAtPoint:securityDotDrawStartPoint]; } else { // 驗(yàn)證碼,顯示數(shù)字 // 遍歷驗(yàn)證碼/密碼的每個(gè)字符 NSString *charecterString = [NSString stringWithFormat:@"%c", [self.text characterAtIndex:i]]; // 設(shè)置驗(yàn)證碼/密碼的現(xiàn)實(shí)屬性 NSMutableDictionary *attributes = [[NSMutableDictionary alloc] init]; attributes[NSFontAttributeName] = self.font; // 計(jì)算每位驗(yàn)證碼/密碼的繪制起點(diǎn)(為了使驗(yàn)證碼/密碼位于tempRect的中部,不應(yīng)該從tempRect的重點(diǎn)開(kāi)始繪制) // 計(jì)算每位驗(yàn)證碼/密碼的在指定樣式下的size CGSize characterSize = [charecterString sizeWithAttributes:attributes]; CGPoint vertificationCodeDrawStartPoint = CGPointMake(width * i + (width - characterSize.width) / 2.0, (tempRect.size.height - characterSize.height) / 2.0); // 繪制驗(yàn)證碼/密碼 [charecterString drawAtPoint:vertificationCodeDrawStartPoint withAttributes:attributes]; } } }
6、使用示例
在控制器中創(chuàng)建,并設(shè)置vertificationCodeInputView相關(guān)屬性
- (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor lightGrayColor]; self.vertificationCodeInputView = [[IDVertificationCodeInputView alloc] initWithFrame:CGRectMake(50, 250, 200, 45)]; self.vertificationCodeInputView.backgroudImageName = @"1"; // 驗(yàn)證碼(顯示數(shù)字) self.vertificationCodeInputView.secureTextEntry = NO; //self.vertificationCodeInputView.secureTextEntry = YES; [self.view addSubview:self.vertificationCodeInputView]; }
點(diǎn)擊屏幕,打印驗(yàn)證碼的內(nèi)容
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { NSLog(@"%@", self.vertificationCodeInputView.vertificationCode); }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助。
相關(guān)文章
iOS 控件封裝(又名擰螺絲)之排序按鈕的開(kāi)發(fā)
排序按鈕是實(shí)際開(kāi)發(fā)中比較常見(jiàn)的一種控件,這篇文章主要介紹了iOS 開(kāi)發(fā)之排序按鈕,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-12-12iOS實(shí)現(xiàn)獲取系統(tǒng)iTunes音樂(lè)的方法示例
這篇文章主要給大家介紹了關(guān)于iOS如何實(shí)現(xiàn)獲取系統(tǒng)iTunes音樂(lè)的相關(guān)資料,文中通過(guò)示例代碼給大家詳細(xì)介紹了實(shí)現(xiàn)的方法,并給大家介紹了MPMediaPickerController的相關(guān)知識(shí),對(duì)大家的學(xué)習(xí)或者工作具有一定的幫助,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-11-11React Native學(xué)習(xí)教程之自定義NavigationBar詳解
這篇文章主要給大家介紹了關(guān)于React Native學(xué)習(xí)教程之自定義NavigationBar的相關(guān)資料,文中通過(guò)是示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-10-10iOS開(kāi)發(fā)中最基本的位置功能實(shí)現(xiàn)示例
這篇文章主要介紹了iOS開(kāi)發(fā)中最基本的位置功能實(shí)現(xiàn)示例,需要的朋友可以參考下2015-09-09禁止iPhone Safari video標(biāo)簽視頻自動(dòng)全屏的辦法
本篇文章給大家分析有沒(méi)有辦法禁止iPhone Safari video標(biāo)簽視頻自動(dòng)全屏,以下給出好多種情況分享,感興趣的朋友可以參考下2015-09-09iOS開(kāi)發(fā)中用imageIO漸進(jìn)加載圖片及獲取exif的方法
這篇文章主要介紹了iOS開(kāi)發(fā)中中用imageIO漸進(jìn)加載圖片及獲取exif的方法,代碼演示為傳統(tǒng)的Objective-C,需要的朋友可以參考下2015-09-09深入分析iOS應(yīng)用中對(duì)于圖片緩存的管理和使用
這篇文章主要介紹了iOS應(yīng)用中對(duì)于圖片緩存的管理和使用,實(shí)例代碼為傳統(tǒng)的Objective-C,需要的朋友可以參考下2016-04-04iOS開(kāi)發(fā)之一些實(shí)用小知識(shí)點(diǎn)總結(jié)
這篇文章主要給大家介紹了關(guān)于iOS開(kāi)發(fā)之實(shí)用小知識(shí)點(diǎn)的相關(guān)資料,其中包括防止UIButton,cell等重復(fù)點(diǎn)擊、獲取當(dāng)前視圖最頂層的ViewController以及代碼截圖相關(guān)的等知識(shí),需要的朋友可以參考借鑒,下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-10-10