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

自定义键盘的关键: self.textField.inputView = [自定义的view],

支持长按一直删除

demo地址: https://github.com/NieYinlong/NYL_IDCardKeyBoard/tree/master

开始自定义

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

iOS自定义身份证键盘相关推荐

  1. android自定义键盘 自定义身份证键盘

    android 系统键盘支持的点已经比较丰富了, 但是有时候某一些需求还不能满足我们的需求.最近公司应用到了实名认证相关的功能,这部分需要一个身份证的EditText, 自然也需要一个身份证的键盘,奈 ...

  2. IOS 自定义软键盘功能,修改换行键为发送键

    IOS项目是使用混合模式开发,在开发聊天功能时:发现软键盘不能像QQ.微信那样,换行键不能变为发送:网上说是因为输入框类别导致:尝试过以后,还是不行:然后想到用IOS native解决: 先说一下,原 ...

  3. iOS开发中键盘样式和自定义键盘。

    文章目录 系统自带的样式 自定义键盘 在系统自带键盘基础上自定义键盘 完全自定义键盘 自定义全部类型键盘 系统自带的样式 在iOS开发中系统自带键盘已经有很多样式,但是有时候并不能满足我们都开发需求, ...

  4. vue实现自定义身份证,数字键盘(光标,输入框,键盘)

    vue实现自定义身份证,数字键盘(光标,输入框,键盘) 组件介绍 组件代码 效果图 组件使用 引用 使用 参数介绍 方法 插槽 组件介绍 vue实现自定义身份证键盘(光标,输入框,键盘全手写) 组件代 ...

  5. 在iOS8 下用Swift 创建自定义的键盘

    本文翻译自How to make a custom keyboard in iOS 8 using Swift 我将讲解一些关于键盘扩展的基本知识,然后使用iOS 8 提供的新应用扩展API来创建一个 ...

  6. [译] 用 Swift 创建自定义的键盘

    本文翻译自 How to make a custom keyboard in iOS 8 using Swift 我将讲解一些关于键盘扩展的基本知识,然后使用iOS 8 提供的新应用扩展API来创建一 ...

  7. vue封装自定义数字键盘组件

    最近在公司做一个项目,简单来说就是web端微信公众号上的一个申请表单页面,随便如个图 环境: vue框架 + vant组件库 + 其它(这里用不上的,不说) 具体如图: 都知道,input输入框如果不 ...

  8. android 自定义键盘的安全性,自定义安全键盘——仿民生银行

    系统自带键盘和第三方的键盘不管是从性能还是从体验上来说都要胜于我们自己写的,但我们为什么还要去自定义键盘呢?其实就为了安全性,比如用户在输入账户密码,支付密码的时候,防止键盘获取到我们的数据:或者说美 ...

  9. 【uniapp前端组件】自定义车牌键盘

    自定义车牌输入键盘–车牌键盘 简介 本组件根据自定义万能键盘(数字键盘.身份证键盘.带小数点数字键盘.车牌键盘)升级而来,老组件代码有点看不懂了,哈哈哈哈.另外数字键盘.身份证键盘.小数点数字键盘un ...

最新文章

  1. antd 验证 动态 required_3分钟短文:十年窖藏,Laravel告诉你表单验证的正确姿势
  2. PNAS | 开发用于优化蛋白质设计的3D模型
  3. python一个月能学成嘛-0基础学Python,1个月写爬虫,走了哪些弯路?
  4. python画切片图_python|Python图片常用操作-索引与切片
  5. memcpy函数_[PART][BUG][MSVCRT][C][CCF NOI1097] 关于memcpy的坑
  6. 如何让new操作符只构造,不申请内存
  7. 线性表之链式存储结构_单链表相关算法
  8. 用大白话带你理解CPU指令集
  9. 计算机图形学的网络课程
  10. %几.几//C语言(闲的没事,记录下)
  11. 新存储、新格局、新飞跃,浪潮存储应时而来
  12. MindSpore21天实战营(2):基于BERT实现中文新闻分类实战
  13. 无法同步谷歌日历_安卓手机的日历App之选择、使用(附记:纪念日App)
  14. 【16.8】苹果四代蓝牙耳机+吉萌兔保温杯+小红杯拿铁黑咖啡+公务员教材真题
  15. Python输入账号密码判断是否正确并输出,典型案例-百钱买百鸡的两个程序代码
  16. FPGA——时钟分频
  17. 初中化学人教版教案二-Leo老师
  18. 给你一本武林秘籍,和KeeWiDB一起登顶高性能
  19. Java丨即时聊天程序的实现
  20. Python虽然很火,为啥找工作这么难

热门文章

  1. BeanUtils.populate()用法
  2. 任意阶幻方的解法及c++实现
  3. 解决阿里巴巴JSONObject工具 com.alibaba.fastjson.JSONObject cannot be cast to 的问题
  4. CRMEB多商户系统怎么设置跳转链接
  5. three.js创建简单的凹凸贴图
  6. 如何使用自己的云服务器做代理服务器
  7. 百度网盘如何在线播放电影?
  8. Bandizip 7.16和winRAR在文件右键压缩功能上的差异
  9. python中常见的错误提示_python常见异常提示
  10. python爬取微信公众号文章(包含文章内容和图片)