iOS自定义身份证键盘

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

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

(0)

相关推荐

  • iOS中键盘 KeyBoard 上添加工具栏的方法

    iOS中 键盘 KeyBoard 上怎么添加工具栏? 如图中所示 在键盘上面加一条工具栏 大致思路是提前创建好工具栏,在键盘弹出的时候将工具栏显示出来,在键盘消失的时候让工具栏隐藏 上代码 设置两个变量 UIView * _toolView; //工具栏 UITextField *textField;// 输入框 呼出键盘用 创建工具栏 输入框 添加键盘弹出 消失的通知 - (void)viewDidLoad { [super viewDidLoad]; // Do any additional

  • iOS中只让textField使用键盘通知的实例代码

    代码: #import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. //为textField增加键盘事件 [[NSNotificati

  • IOS数字键盘左下角添加完成按钮的实现方法

    IOS数字键盘左下角添加完成按钮的实现方法 实现代码: - (void)addDoneButtonToNumPadKeyboard { UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom]; if (systemVersion < 8.0){ doneButton.frame = CGRectMake(0, 163, 106, 53); }else{ doneButton.frame = CGRectMake(0,

  • iOS实现输入框跟随键盘自动上移的实例代码

    场景还原 有些时候在包含输入框的页面中,点击输入框输入会因键盘弹起而遮挡住一部分输入框,影响用户体验.iOS在默认情况下并不会处理这种问题,不过我们可以自己实现键盘弹起输入框自动上移的效果. 实现思路 观察键盘的弹起与收回,当弹起的键盘会遮挡住输入框时,将输入框跟随键盘一并上移合适的距离,当键盘收回时,输入框回到原始状态. 具体方案 1. 注册两个观察者,观察键盘的弹起与收回 [[NSNotificationCenter defaultCenter] addObserver:self selec

  • iOS开发第三方键盘处理实例代码

    最近项目中遇到了键盘处理通知被调用多次的情况,废了好半天时间才找到解决办法,今天就给小伙伴儿们唠唠第三方键盘处理的那些坑! 详情请看:『https://github.com/boai/BAKeyboardDemo』 ! 1.聊天评论框的封装 先聊聊我项目中遇到的奇葩情况吧,一个直播界面,上面播放器,下面是分段控制器5个button,5个界面,其中三个界面最下面都是评论框,所以就封装了一个评论框公用. 但是本来用的『IQKeyboardManager』,开源键盘处理框架,可是在同一个界面有多个评论

  • h5 ios输入框和键盘的兼容性优化指南

    起因 h5的输入框引起键盘导致体验不好,目前就算微信.知乎.百度等产品也没有很好的技术方案实现,尤其底部固定位置的输入框各种方案都用的前提下体验也并没有很好,这个问题也是老大难问题了.目前在准备一套与native协议 来解决这个问题,目前项目中的解决方案还是有值得借鉴的地方的,分享一下 下面话不多说了,来一起看看详细的介绍吧 业务场景 固定在h5页面底部的输入框 无论是使用 <input /> 还是 <div contenteditable="true"> &l

  • iOS中的UIKeyboard键盘视图使用方法小结

    一.键盘风格   UIKit框架支持8种风格键盘. 复制代码 代码如下: typedef enum {      UIKeyboardTypeDefault,                // 默认键盘:支持所有字符      UIKeyboardTypeASCIICapable,           // 支持ASCII的默认键盘      UIKeyboardTypeNumbersAndPunctuation,  // 标准电话键盘,支持+*#等符号      UIKeyboardType

  • iOS项目开发键盘弹出遮挡输入框问题解决方案

    在iOS或Android等移动端开发过程中,经常遇到很多需要我们输入信息的情况,例如登录时要输入账号密码.查询时要输入查询信息.注册或申请时需要填写一些信息等都是通过我们键盘来进行输入的,在iOS开发过程中,一般用于进行输入信息的有两类:UITextField和UITextView,前者是单行输入文本框,后者是可滑动的多行输入文本框,在这整个开发过程中,我们需要控制键盘的弹出和收起.在输入结束的时候获取输入的信息,此外,我们还需要保证在键盘弹起的时候不遮挡我们输入的文本框.今天,我们就主要来说一

  • iOS自定义键盘切换效果

    本文实例为大家分享了iOS自定义键盘切换的相关代码,供大家参考,具体内容如下 具体代码如下 - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. self.title = @"小飞哥键盘"; self.textField = [[UITextField alloc] initWithFrame:CGRectMa

  • iOS应用开发中监听键盘事件的代码实例小结

    1.注册监听键盘事件的通知 复制代码 代码如下: [[NSNotificationCenter defaultCenter] addObserver:self                                              selector:@selector(keyboardWillShow:)                                                  name:UIKeyboardWillShowNotification   

随机推荐