iOS自定義身份證鍵盤
本文實(shí)例為大家分享了iOS自定義身份證鍵盤的具體代碼,供大家參考,具體內(nèi)容如下
項(xiàng)目中有需要需要身份證的輸入框, 用自帶的輸入切換很麻煩(如果最后一位帶X), 所以自定義一個(gè)身份證輸入鍵盤.
自定義鍵盤的關(guān)鍵: self.textField.inputView = [自定義的view],
支持長(zhǎng)按一直刪除
demo地址
開始自定義
1. 創(chuàng)建一個(gè)集成自UIView的視圖 (NYLIDKeyBoard)
NYLIDKeyBoard.h
// // NYLIDKeyBoard.h // lqz // // Created by 聶銀龍 on 2017/9/7. // Copyright © 2017年 lqz. All rights reserved. // 身份證鍵盤 #import <UIKit/UIKit.h> @class NYLIDKeyBoard; @protocol NYKIDKeyBoardDelegate <NSObject> @optional /** 點(diǎn)擊按鈕代理回調(diào) @param idKeyboard 本類 @param inputString 點(diǎn)擊按鈕拼接后的字符串 */ - (void)idKeyboard:(NYLIDKeyBoard *)idKeyboard inputSring:(NSMutableString *)inputString; @end @interface NYLIDKeyBoard : UIView @property(nonatomic, assign) id<NYKIDKeyBoardDelegate>delegate; // 輸入的字符串 @property(nonatomic, strong) NSMutableString *inputString; @end
NYLIDKeyBoard.m
// // NYLIDKeyBoard.m // lqz // // Created by 聶銀龍 on 2017/9/7. // Copyright © 2017年 lqz. All rights reserved. // #import "NYLIDKeyBoard.h" #define RGB(r,g,b) [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1.0] // 屏幕高度 #define SCREEN_HEIGHT [[UIScreen mainScreen] bounds].size.height // 屏幕寬度 #define SCREEN_WIDTH [[UIScreen mainScreen] bounds].size.width #define GETSIZE(num) (SCREEN_WIDTH/375*num) @implementation NYLIDKeyBoard /* // Only override drawRect: if you perform custom drawing. // An empty implementation adversely affects performance during animation. - (void)drawRect:(CGRect)rect { // Drawing code } */ - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { self.inputString = [NSMutableString string]; [self initViewFrame:frame]; } return self; } - (void)initViewFrame:(CGRect)frame { self.userInteractionEnabled = YES; CGFloat width = frame.size.width; CGFloat height = frame.size.height; // // UIView *topBgView = nil; // topBgView = [[UIView alloc] initWithFrame:CGRectMake(-1, 0, width +2, 40)]; // topBgView.backgroundColor = RGB(249, 249, 249);//[UIColor colorWithWhite:0.92 alpha:0.92]; // topBgView.userInteractionEnabled = YES; // topBgView.layer.borderColor = RGB(214, 213, 214).CGColor; // topBgView.layer.borderWidth = 0.6; // topBgView.alpha = 0.99; // [self addSubview:topBgView]; // // UIButton *okBtn = [UIButton buttonWithType:(UIButtonTypeCustom)]; // okBtn.frame = CGRectMake(SCREEN_WIDTH-50-4, 0, 50, 40); // [okBtn setTitle:@"完成" forState:(UIControlStateNormal)]; // [okBtn setTitleColor:BASE_BACKGROUNG_BLUE_COLOR forState:(UIControlStateNormal)]; // [okBtn setTitleColor:[UIColor blueColor] forState:(UIControlStateHighlighted)]; // [topBgView addSubview:okBtn]; // [okBtn addTarget:self action:@selector(okbtnClick) forControlEvents:(UIControlEventTouchUpInside)]; NSInteger totalColumns = 3; // 總列數(shù) CGFloat cellW = width/3; // 每個(gè)格子的寬度 CGFloat cellH = GETSIZE(54); // 格子高度 NSArray *titles = @[@"1", @"2", @"3", @"4", @"5", @"6", @"7", @"8", @"9", @"X", @"0", @""]; for (int i = 0; i < titles.count ; i++) { int row = i / totalColumns; // 行 int col = i % totalColumns; // 列 //根據(jù)行號(hào)和列號(hào)來確定 子控件的坐標(biāo) CGFloat cellX = col * cellW; CGFloat cellY = row * cellH; UIButton *btn = [UIButton buttonWithType:(UIButtonTypeCustom)]; btn.frame = CGRectMake(cellX, cellY, cellW, cellH); [btn setTitle:titles[i] forState:(UIControlStateNormal)]; btn.titleLabel.font = [UIFont boldSystemFontOfSize:20]; [btn setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)]; [btn setBackgroundImage:[UIImage imageNamed:@"nyl_keyboard_white"] forState:(UIControlStateNormal)]; [btn setBackgroundImage:[UIImage imageNamed:@"nyl_keyboard"] forState:(UIControlStateHighlighted)]; [self addSubview:btn]; btn.tag = 100 + i; //NSLog(@"%.2f === %.2f == %.2f", btn.left, cellX, btn.bottom); [btn addTarget:self action:@selector(actionBtnClick:) forControlEvents:(UIControlEventTouchUpInside)]; if (btn.tag == 111) { // 刪除按鈕 //button長(zhǎng)按事件 UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(btnLong:)]; //longPress.minimumPressDuration = ; //定義按的時(shí)間 [btn addGestureRecognizer:longPress]; // 刪除按鈕上面加圖片 UIImageView *delImageV = [[UIImageView alloc] init]; delImageV.image = [UIImage imageNamed:@"nylKeyBoard_del"]; CGFloat img_width = cellW / 4.6; CGFloat img_height = img_width * 30 / 40; // 比例高度 delImageV.frame = CGRectMake( (cellW - img_width) / 2, (cellH - img_height) / 2, img_width, img_height); [btn addSubview:delImageV]; } } //CGFloat topBottom = topBgView.bottom; // 豎線 for (int i = 0; i < 2; i++) { UIView *line = [[UIView alloc] initWithFrame:CGRectMake(cellW + i * (cellW), 0, 0.5, height)]; line.backgroundColor = RGB(214, 213, 214); [self addSubview:line]; } // 橫線 for (int i = 0; i < 3; i++) { UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, cellH+ i * cellH, width, 0.5)]; line.backgroundColor = RGB(214, 213, 214); [self addSubview:line]; } } - (void)okbtnClick { [self removeFromSuperview]; if (_delegate && [_delegate respondsToSelector:@selector(idKeyboard:inputSring:)]) { [_delegate idKeyboard:self inputSring:self.inputString]; } } - (void)actionBtnClick:(UIButton *)btn { NSLog(@"自定義鍵盤按鈕方法===== %@", btn.titleLabel.text); if (btn.tag == 111 && self.inputString.length > 0) { [self.inputString deleteCharactersInRange:NSMakeRange(self.inputString.length-1, 1)]; } else { if (btn.tag != 111) { [self.inputString appendString:btn.titleLabel.text]; } } if (_delegate && [_delegate respondsToSelector:@selector(idKeyboard:inputSring:)]) { [_delegate idKeyboard:self inputSring:self.inputString]; } } #pragma mark - 長(zhǎng)按鈕刪除 -(void)btnLong:(UILongPressGestureRecognizer *)gestureRecognizer{ if (self.inputString.length > 0) { [self.inputString deleteCharactersInRange:NSMakeRange(self.inputString.length-1, 1)]; NSLog(@"長(zhǎng)按==== %@", self.inputString); if (_delegate && [_delegate respondsToSelector:@selector(idKeyboard:inputSring:)]) { [_delegate idKeyboard:self inputSring:self.inputString]; } } } @end
在controller中使用
// // ViewController.m // NYL_IDCardKeyBoard // // Created by 聶銀龍 on 2017/9/8. // Copyright © 2017年 聶銀龍. All rights reserved. // #import "ViewController.h" #import "NYLIDKeyBoard.h" // 屏幕高度 #define SCREEN_HEIGHT [[UIScreen mainScreen] bounds].size.height // 屏幕寬度 #define SCREEN_WIDTH [[UIScreen mainScreen] bounds].size.width #define GETSIZE(num) (SCREEN_WIDTH/375*num) @interface ViewController ()<NYKIDKeyBoardDelegate> @property (weak, nonatomic) IBOutlet UITextField *textField; @property(nonatomic, strong) NYLIDKeyBoard *idKeyBoard; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // 設(shè)置自定義鍵盤 self.textField.inputView = self.idKeyBoard; } #pragma mark - 輸入代理回調(diào) - (void)idKeyboard:(NYLIDKeyBoard *)idKeyboard inputSring:(NSMutableString *)inputString { _textField.text = inputString; } - (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { [self.textField resignFirstResponder]; } // 身份證鍵盤 - (NYLIDKeyBoard *)idKeyBoard { if (!_idKeyBoard) { _idKeyBoard = [[NYLIDKeyBoard alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT - GETSIZE(216), SCREEN_WIDTH, GETSIZE(216) )]; _idKeyBoard.delegate = self; } return _idKeyBoard; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
IOS應(yīng)用內(nèi)跳轉(zhuǎn)系統(tǒng)設(shè)置相關(guān)界面的方法
在iOS開發(fā)中,有時(shí)會(huì)有跳轉(zhuǎn)系統(tǒng)設(shè)置界面的需求,例如提示用戶打開藍(lán)牙或者WIFI,提醒用戶打開推送或者位置權(quán)限等,接下來通過本文給大家介紹IOS應(yīng)用內(nèi)跳轉(zhuǎn)系統(tǒng)設(shè)置相關(guān)界面的方法,喜歡的朋友參考下2016-02-02iOS實(shí)現(xiàn)“搖一搖”與“掃一掃”功能示例代碼
本篇文章主要介紹了iOS實(shí)現(xiàn)“搖一搖”與“掃一掃”功能示例代碼,具有一定的參考價(jià)值,有興趣的可以了解一下。2017-01-01你應(yīng)該知道的tableViewCell行高計(jì)算處理
這篇文章主要給大家介紹了關(guān)于tableViewCell行高計(jì)算的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-12-12