1. 程式人生 > 程式設計 >iOS自定義身份證鍵盤

iOS自定義身份證鍵盤

本文例項為大家分享了iOS自定義身份證鍵盤的具體程式碼,供大家參考,具體內容如下

專案中有需要需要身份證的輸入框,用自帶的輸入切換很麻煩(如果最後一位帶X),所以自定義一個身份證輸入鍵盤.

自定義鍵盤的關鍵:self.textField.inputView = [自定義的view],

支援長按一直刪除

demo地址

iOS自定義身份證鍵盤

開始自定義

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,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,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.5,height)];
  line.backgroundColor = RGB(214,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,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

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。