iOS自定义身份证键盘

时间:2021-10-17 08:51:47

本文实例为大家分享了ios自定义身份证键盘的具体代码,供大家参考,具体内容如下

项目中有需要需要身份证的输入框, 用自带的输入切换很麻烦(如果最后一位带x), 所以自定义一个身份证输入键盘.

自定义键盘的关键: self.textfield.inputview = [自定义的view], 

支持长按一直删除

demo地址

iOS自定义身份证键盘

开始自定义

1. 创建一个集成自uiview的视图 (nylidkeyboard)

nylidkeyboard.h

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
//
// 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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
//
// 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中使用

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//
// 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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/NLYNN/article/details/77891072