iOS自定义身份证键盘
程序员文章站
2022-06-21 08:26:00
本文实例为大家分享了ios自定义身份证键盘的具体代码,供大家参考,具体内容如下项目中有需要需要身份证的输入框, 用自带的输入切换很麻烦(如果最后一位带x), 所以自定义一个身份证输入键盘.自定义键盘的...
本文实例为大家分享了ios自定义身份证键盘的具体代码,供大家参考,具体内容如下
项目中有需要需要身份证的输入框, 用自带的输入切换很麻烦(如果最后一位带x), 所以自定义一个身份证输入键盘.
自定义键盘的关键: self.textfield.inputview = [自定义的view],
支持长按一直删除
demo地址
开始自定义
1. 创建一个集成自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 /** 点击按钮代理回调 @param idkeyboard 本类 @param inputstring 点击按钮拼接后的字符串 */ - (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; // 总列数 cgfloat cellw = width/3; // 每个格子的宽度 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; // 列 //根据行号和列号来确定 子控件的坐标 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长按事件 uilongpressgesturerecognizer *longpress = [[uilongpressgesturerecognizer alloc] initwithtarget:self action:@selector(btnlong:)]; //longpress.minimumpressduration = ; //定义按的时间 [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 - 长按钮删除 -(void)btnlong:(uilongpressgesturerecognizer *)gesturerecognizer{ if (self.inputstring.length > 0) { [self.inputstring deletecharactersinrange:nsmakerange(self.inputstring.length-1, 1)]; nslog(@"长按==== %@", 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]; // 设置自定义键盘 self.textfield.inputview = self.idkeyboard; } #pragma mark - 输入代理回调 - (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
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。