欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

iOS自定義身份證鍵盤

 更新時(shí)間:2020年05月26日 15:50:48   作者:--風(fēng)起云涌--  
這篇文章主要為大家詳細(xì)介紹了iOS自定義身份證鍵盤,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了iOS自定義身份證鍵盤的具體代碼,供大家參考,具體內(nèi)容如下

項(xiàng)目中有需要需要身份證的輸入框, 用自帶的輸入切換很麻煩(如果最后一位帶X), 所以自定義一個(gè)身份證輸入鍵盤.

自定義鍵盤的關(guān)鍵: self.textField.inputView = [自定義的view], 

支持長(zhǎng)按一直刪除

demo地址

開始自定義

1. 創(chuàng)建一個(gè)集成自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
 
/**
 點(diǎn)擊按鈕代理回調(diào)
 @param idKeyboard 本類
 @param inputString 點(diǎn)擊按鈕拼接后的字符串
 */
- (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;  // 總列數(shù)
 CGFloat cellW = width/3; // 每個(gè)格子的寬度
 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; // 列
  //根據(jù)行號(hào)和列號(hào)來確定 子控件的坐標(biāo)
  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長(zhǎng)按事件
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(btnLong:)];
    //longPress.minimumPressDuration = ; //定義按的時(shí)間
   [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 - 長(zhǎng)按鈕刪除
-(void)btnLong:(UILongPressGestureRecognizer *)gestureRecognizer{
 
 if (self.inputString.length > 0) {
  [self.inputString deleteCharactersInRange:NSMakeRange(self.inputString.length-1, 1)];
  
  NSLog(@"長(zhǎng)按==== %@", 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];
 
 // 設(shè)置自定義鍵盤
 self.textField.inputView = self.idKeyBoard;
 
 
}
 

#pragma mark - 輸入代理回調(diào)
- (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

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • iOS 圖片裁剪 + 旋轉(zhuǎn)

    iOS 圖片裁剪 + 旋轉(zhuǎn)

    之前分別介紹了圖片裁剪和圖片旋轉(zhuǎn)的方法,裁剪和旋轉(zhuǎn)是可以連在一起執(zhí)行的。本文將對(duì)此進(jìn)行介紹,具有很好的參考價(jià)值。下面跟著小編一起來看下吧
    2017-03-03
  • IOS應(yīng)用內(nèi)跳轉(zhuǎn)系統(tǒng)設(shè)置相關(guān)界面的方法

    IOS應(yīng)用內(nèi)跳轉(zhuǎn)系統(tǒng)設(shè)置相關(guān)界面的方法

    在iOS開發(fā)中,有時(shí)會(huì)有跳轉(zhuǎn)系統(tǒng)設(shè)置界面的需求,例如提示用戶打開藍(lán)牙或者WIFI,提醒用戶打開推送或者位置權(quán)限等,接下來通過本文給大家介紹IOS應(yīng)用內(nèi)跳轉(zhuǎn)系統(tǒng)設(shè)置相關(guān)界面的方法,喜歡的朋友參考下
    2016-02-02
  • iOS繪制3D餅圖的實(shí)現(xiàn)方法

    iOS繪制3D餅圖的實(shí)現(xiàn)方法

    餅圖常用于統(tǒng)計(jì)學(xué)模塊。常見的一般為2D餅圖,這篇文章主要介紹了iOS繪制3D餅圖的實(shí)現(xiàn)方法,3D餅圖更加立體,用戶的好感度也比較高,下面需要的朋友可以參考借鑒,一起來看看吧。
    2017-01-01
  • iOS彈幕組件LNDanmakuMaster的具體使用

    iOS彈幕組件LNDanmakuMaster的具體使用

    這篇文章主要介紹了iOS彈幕組件LNDanmakuMaster的具體使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-02-02
  • iOS實(shí)現(xiàn)圖片折疊效果

    iOS實(shí)現(xiàn)圖片折疊效果

    這篇文章主要為大家詳細(xì)介紹了iOS實(shí)現(xiàn)圖片折疊效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-02-02
  • iOS實(shí)現(xiàn)“搖一搖”與“掃一掃”功能示例代碼

    iOS實(shí)現(xiàn)“搖一搖”與“掃一掃”功能示例代碼

    本篇文章主要介紹了iOS實(shí)現(xiàn)“搖一搖”與“掃一掃”功能示例代碼,具有一定的參考價(jià)值,有興趣的可以了解一下。
    2017-01-01
  • 你應(yīng)該知道的tableViewCell行高計(jì)算處理

    你應(yīng)該知道的tableViewCell行高計(jì)算處理

    這篇文章主要給大家介紹了關(guān)于tableViewCell行高計(jì)算的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-12-12
  • iOS實(shí)現(xiàn)代碼只執(zhí)行一次

    iOS實(shí)現(xiàn)代碼只執(zhí)行一次

    本文給大家分享的是在iOS中控制代碼在整個(gè)軟件生命周期中只運(yùn)行一次的代碼,有需要的小伙伴可以參考下。
    2016-03-03
  • iOS基礎(chǔ)動(dòng)畫教程分享

    iOS基礎(chǔ)動(dòng)畫教程分享

    這篇文章主要為大家詳細(xì)介紹了iOS幾種基礎(chǔ)動(dòng)畫教程,包括位置動(dòng)畫、透明度動(dòng)畫、大小動(dòng)畫等,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-11-11
  • iOS推送的那些事

    iOS推送的那些事

    關(guān)于iOS推送的那些事,你知道多少?本文帶著大家一起了解iOS推送,感興趣的小伙伴們可以參考一下
    2016-02-02

最新評(píng)論