自定义身份证输入键盘
iOS开发过程中难免会遇到各种自定义的视图和界面,原生系统不可能提供所有的雷同界面,就算键盘样式有8种,也满足不了产品各种变态的需求,今天提供一种输入时身份证专属键盘,讲白了就是自定义一个带x 的键盘,网上也有各种大神写的sdk,但是总感觉麻烦,废话免谈,都是程序猿,且看如下代码:
1、引用DefineIdentityKeyboardView文件内容(就一个文件,包含.h和.m)
/***.h内容如下***/
#import <UIKit/UIKit.h>
@interface DefineIdentityKeyboardViewCell : UICollectionViewCell
@end
@interface DefineIdentityKeyboardView : UIView
@end
/***.m内容如下***/
#import “DefineIdentityKeyboardView.h”
@interface DefineIdentityKeyboardViewCell()
@property(nonatomic,strong)UIButton *titleBtn;
@property(nonatomic,strong)UIImageView *bottomImg;
@property(nonatomic,strong)UIImageView *rightImg;
@end
@implementation DefineIdentityKeyboardViewCell
-(id)initWithFrame:(CGRect)frame
{
self=[super initWithFrame:frame];
if (self)
{
[self setUpContent];
}
return self;
}
-(void)setUpContent
{
_titleBtn=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
_titleBtn.userInteractionEnabled=NO;
_titleBtn.titleLabel.font =[UIFont boldSystemFontOfSize:20];
[_titleBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[self addSubview:_titleBtn];
_bottomImg=[[UIImageView alloc]initWithFrame:CGRectMake(0, self.frame.size.height-1, self.frame.size.width, 1)];
_bottomImg.backgroundColor=[[UIColor grayColor] colorWithAlphaComponent:0.4];
[self addSubview:_bottomImg];_rightImg=[[UIImageView alloc]initWithFrame:CGRectMake(self.frame.size.width-1, 0, 1, self.frame.size.height)];
_rightImg.backgroundColor=[[UIColor grayColor] colorWithAlphaComponent:0.4];
[self addSubview:_rightImg];
}
@end
/**/
@interface DefineIdentityKeyboardView()<UICollectionViewDelegate,UICollectionViewDataSource>
@property(nonatomic,strong)UICollectionView *collectionView;
@property(nonatomic,strong)NSArray *dataArrary;
@end
@implementation DefineIdentityKeyboardView
-(id)initWithFrame:(CGRect)frame
{
self=[super initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height-250, [UIScreen mainScreen].bounds.size.width, 250)];
if (self)
{
_dataArrary=@[@“1”,@“2”,@“3”,@“4”,@“5”,@“6”,@“7”,@“8”,@“9”,@“X”,@“0”,@“删除”];
//
[self setUpContent];
}
return self;
}
-(void)setUpContent
{
self.backgroundColor=[UIColor redColor];
//
UICollectionViewFlowLayout *flowlayout=[UICollectionViewFlowLayout new];
flowlayout.scrollDirection=UICollectionViewScrollDirectionVertical;
flowlayout.itemSize=CGSizeMake([UIScreen mainScreen].bounds.size.width/3, 45);
flowlayout.minimumLineSpacing=0;
flowlayout.minimumInteritemSpacing=0;
_collectionView=[[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) collectionViewLayout:flowlayout];
_collectionView.dataSource=self;
_collectionView.delegate=self;
_collectionView.backgroundColor=[UIColor whiteColor];
[self addSubview:_collectionView];
[_collectionView registerClass:[DefineIdentityKeyboardViewCell class] forCellWithReuseIdentifier:@"cell"];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return _dataArrary.count;
} - (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
DefineIdentityKeyboardViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@“cell” forIndexPath:indexPath];
cell.titleBtn.titleLabel.font=indexPath.row==11?[UIFont systemFontOfSize:13]:[UIFont boldSystemFontOfSize:20];
[cell.titleBtn setTitle:_dataArrary[indexPath.row] forState:UIControlStateNormal];
cell.bottomImg.hidden=indexPath.row<9?NO:YES;
return cell;
}
(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{[[NSNotificationCenter defaultCenter]postNotificationName:@“name” object:nil userInfo:@{@“number”:_dataArrary[indexPath.row]}];
}
2、调用方式
a、通知接受输入结果
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(numerAction:) name:@“name” object:nil];
b、 引用
_numberTextField=[[UITextField alloc]initWithFrame:CGRectMake(80, 100, ScreenWidth-160, 40)];
_numberTextField.delegate=self;
_numberTextField.placeholder=@“请输入您的身份证号”;
_numberTextField.font=[UIFont systemFontOfSize:14];
_numberTextField.layer.borderWidth=1;
_numberTextField.layer.borderColor=linecolor.CGColor;
_numberTextField.textAlignment=NSTextAlignmentCenter;
_numberTextField.keyboardType=UIKeyboardTypePhonePad;
_numberTextField.inputView=[[DefineIdentityKeyboardView alloc]init];
[self.view addSubview:_numberTextField];
c、方法处理
-(void)numerAction:(NSNotification *)notification
{
if ([[notification.userInfo objectForKey:@“number”] isEqualToString:@“删除”]){
if (_numberTextField.text.length>0){
_numberTextField.text=[_numberTextField.text substringToIndex:_numberTextField.text.length-1]; }
}else{
_numberTextField.text=[NSString stringWithFormat:@"%@%@",_numberTextField.text,[notification.userInfo objectForKey:@“number”]];}
}
总结:是不是很简单,讲白了就是自定一个键盘,替换系统的,核心语句我加粗了,注意点:1、先注册通知,后调用,不然收不到输入结果 2、删除按钮的文字或者图片都可以自定义,点击动画暂无,有需要的后面我再更新,感谢浏览,有更好的方法可私聊,共同进步
自定义身份证输入键盘相关推荐
- iOS自定义身份证键盘
项目中有需要需要身份证的输入框, 用自带的输入切换很麻烦(如果最后一位带X), 所以自定义一个身份证输入键盘. 自定义键盘的关键: self.textField.inputView = [自定义的vi ...
- vue实现自定义身份证,数字键盘(光标,输入框,键盘)
vue实现自定义身份证,数字键盘(光标,输入框,键盘) 组件介绍 组件代码 效果图 组件使用 引用 使用 参数介绍 方法 插槽 组件介绍 vue实现自定义身份证键盘(光标,输入框,键盘全手写) 组件代 ...
- 基于struts2框架-自定义身份证号验证器
自定义拦截器的步骤: 1.定义一个验证器的类: > 自定义的验证器都需要实现 Validator接口. > 可以选择继承 ValidatorSupport 或 FieldValidato ...
- LVGL8之自定义密码输入键盘
这一篇研究一下自定义密码输入键盘,还是通过codeblock来模拟代码的运行,代码如下: #include <stdio.h> #include <string.h> #inc ...
- Android 自定义相机 身份证拍照 自定义身份证相机
项目中需要用到拍摄身份证,拍完照片后直接拿到和身份证比例一致的图片,做成功的结果如下: 拍完照后直接拿到裁剪好的图本文的核心技术来自: https://yq.aliyun.com/article ...
- uniapp 微信小程序 - 调起手机摄像头: 拍摄身份证的正反面,自定义身份证取景框,在相机上绘制 “身份证轮廓“ 与提示文字(超详细示例源码,一键复制运行开箱即用)
效果图 这个需求很难在网上找到,就算找到都是很乱的代码.. 本篇博客实现了在uniapp项目中,微信小程序平台下调起手机相机照身份证功能,在相机上添加辅助框.自定义文案或图片(给相机加上身份证辅助线的 ...
- android自定义键盘 自定义身份证键盘
android 系统键盘支持的点已经比较丰富了, 但是有时候某一些需求还不能满足我们的需求.最近公司应用到了实名认证相关的功能,这部分需要一个身份证的EditText, 自然也需要一个身份证的键盘,奈 ...
- 自定义身份证识别相机UI
现在很多的项目都有身份证识别的环节,而系统的相机有时不能满足我们的需要,身份证的识别,有时需要对图片锐化,灰值,这时对于获取图片的尺寸 有为重要,网上很多厂商的SDK 都会把UI这个快 封装起来.我自 ...
- android自定义金额输入键盘_Android 自定义输入支付密码的软键盘实例代码
Android 自定义输入支付密码的软键盘 有项目需求需要做一个密码锁功能,还有自己的软键盘,类似与支付宝那种,这里是整理的资料,大家可以看下,如有错误,欢迎留言指正 需求:要实现类似支付宝的输入支付 ...
最新文章
- eclipse lombok插件安装_Eclipse-安装Freemarker插件
- Cento7+Nginx 之 URL重写
- 我的世界java刷怪数量_我的世界Minecraft源码分析(1):刷怪逻辑
- OpenGL 深度测试depth test 的实例
- 毕业设计之路(2)——初识TCP
- php 1 打印出来,php 怎么强制打印错误
- WordPress博客杂志CMS主题用户中心
- PageAbility基本概念
- mysql去重操作哪个最快_如何将 MySQL 去重操作优化到极致?| CSDN 博文精选
- bootstrap入门
- 把iPad上的视频推送到大麦盒子去
- 显微镜自动聚焦原理是什么_共聚焦显微镜
- 文献 | 柳叶刀发文:虚拟现实的新用途之治疗场所恐惧症
- Halcon条形码识别
- 一个简单易用的Http访问工具类for Android
- rrpp协议如何修改_RRPP快速环网保护协议
- PHP+Mysql——博客系统(前端+后端-整套源码)
- 学习笔记10--CAN总线技术
- 苹果连不上电脑服务器未响应,苹果电脑服务器未响应怎么办
- 大数据学习——Flume入门