当前位置: 移动技术网 > IT编程>移动开发>IOS > iOS自定义身份证键盘

iOS自定义身份证键盘

2020年06月23日  | 移动技术网IT编程  | 我要评论

纪易达,胜似黎明前的琉璃色,清明放几天假

本文实例为大家分享了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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

  • ios uicollectionview实现横向滚动

    现在使用卡片效果的app很多,之前公司让实现一种卡片效果,就写了一篇关于实现卡片的文章。文章最后附有demo实现上我选择了使用uicollectionview ... [阅读全文]
  • iOS UICollectionView实现横向滑动

    本文实例为大家分享了ios uicollectionview实现横向滑动的具体代码,供大家参考,具体内容如下uicollectionview的横向滚动,目前我使... [阅读全文]
  • iOS13适配深色模式(Dark Mode)的实现

    iOS13适配深色模式(Dark Mode)的实现

    好像大概也许是一年前, mac os系统发布了深色模式外观, 看着挺刺激, 时至今日用着也还挺爽的终于, 随着iphone11等新手机的发售, ios 13系统... [阅读全文]
  • ios 使用xcode11 新建项目工程的步骤详解

    ios 使用xcode11 新建项目工程的步骤详解

    xcode11新建项目工程,新增了scenedelegate这个类,转而将原appdelegate负责的对ui生命周期的处理担子接了过来。故此可以理解为:ios... [阅读全文]
  • iOS实现转盘效果

    本文实例为大家分享了ios实现转盘效果的具体代码,供大家参考,具体内容如下demo下载地址: ios转盘效果功能:实现了常用的ios转盘效果,轮盘抽奖效果的实现... [阅读全文]
  • iOS开发实现转盘功能

    本文实例为大家分享了ios实现转盘功能的具体代码,供大家参考,具体内容如下今天给同学们讲解一下一个转盘选号的功能,直接上代码直接看viewcontroller#... [阅读全文]
  • iOS实现轮盘动态效果

    本文实例为大家分享了ios实现轮盘动态效果的具体代码,供大家参考,具体内容如下一个常用的绘图,主要用来打分之类的动画,效果如下。主要是ios的绘图和动画,本来想... [阅读全文]
  • iOS实现九宫格连线手势解锁

    本文实例为大家分享了ios实现九宫格连线手势解锁的具体代码,供大家参考,具体内容如下demo下载地址:效果图:核心代码://// clockview.m// 手... [阅读全文]
  • iOS实现卡片堆叠效果

    本文实例为大家分享了ios实现卡片堆叠效果的具体代码,供大家参考,具体内容如下如图,这就是最终效果。去年安卓5.0发布的时候,当我看到安卓全新的material... [阅读全文]
  • iOS利用余弦函数实现卡片浏览工具

    iOS利用余弦函数实现卡片浏览工具

    本文实例为大家分享了ios利用余弦函数实现卡片浏览工具的具体代码,供大家参考,具体内容如下一、实现效果通过拖拽屏幕实现卡片移动,左右两侧的卡片随着拖动变小,中间... [阅读全文]
验证码:
移动技术网