最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

iOS自定義身份證鍵盤

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

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

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

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

支持長按一直刪除

demo地址

開始自定義

1. 創(chuàng)建一個集成自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; // 每個格子的寬度
 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ù)行號和列號來確定 子控件的坐標(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長按事件
    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];
 
 // 設(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

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

相關(guān)文章

  • iOS實(shí)現(xiàn)動態(tài)的開屏廣告示例代碼

    iOS實(shí)現(xiàn)動態(tài)的開屏廣告示例代碼

    啟動圖是在iOS開發(fā)過程中必不可少的一個部分,很多app在啟動圖之后會有一張自定義的開屏廣告圖,但是有的時候需要讓啟動圖看起來就是一個廣告,而且還要這個廣告里面會動,iOS的啟動圖只能是靜態(tài)的,而且固定,為了實(shí)現(xiàn)看起來的動畫效果,只能進(jìn)行偽造了。下面來一起看看
    2016-09-09
  • 詳解iOS的Core Animation框架中的CATransform3D圖形變換

    詳解iOS的Core Animation框架中的CATransform3D圖形變換

    CATransform3D一般用于操作view的layer的,是Core Animation的結(jié)構(gòu)體,可以用來做比較復(fù)雜的3D操作,這里我們就帶大家來詳解iOS的Core Animation框架中的CATransform3D圖形變換
    2016-07-07
  • iOS10適配以及Xcode8使用需要注意的那些坑

    iOS10適配以及Xcode8使用需要注意的那些坑

    這篇文章主要為大家詳細(xì)介紹了iOS10的適配以及Xcode8使用需要注意的那些坑,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-09-09
  • IOS開發(fā)UIPasteboard類的粘貼板全面詳解

    IOS開發(fā)UIPasteboard類的粘貼板全面詳解

    這篇文章主要為大家介紹了IOS開發(fā)UIPasteboard類的粘貼板全面詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-06-06
  • iOS 禁止按鈕在一定時間內(nèi)連續(xù)點(diǎn)擊

    iOS 禁止按鈕在一定時間內(nèi)連續(xù)點(diǎn)擊

    本文主要介紹了iOS中禁止按鈕在一定時間內(nèi)連續(xù)點(diǎn)擊的方法,具有很好的參考價值,下面跟著小編一起來看下吧
    2017-02-02
  • 設(shè)計模式中的Memento備忘錄模式的在iOS App開發(fā)中的運(yùn)用

    設(shè)計模式中的Memento備忘錄模式的在iOS App開發(fā)中的運(yùn)用

    這篇文章主要介紹了設(shè)計模式中的Memento備忘錄模式的在iOS App開發(fā)中的運(yùn)用,Memento著重于捕獲和具體化當(dāng)前對象的內(nèi)部狀態(tài),需要的朋友可以參考下
    2016-03-03
  • iOS用兩行代碼完美解決數(shù)據(jù)持久化

    iOS用兩行代碼完美解決數(shù)據(jù)持久化

    所謂的持久化,就是將數(shù)據(jù)保存到硬盤中,使得在應(yīng)用程序或機(jī)器重啟后可以繼續(xù)訪問之前保存的數(shù)據(jù)。在iOS開發(fā)中,有很多數(shù)據(jù)持久化的方案,接下來我將嘗試著介紹一種巧妙的方法,用兩行代碼解決這個問題,一起來學(xué)習(xí)下。
    2016-08-08
  • Xcode使用教程詳細(xì)講解(全)

    Xcode使用教程詳細(xì)講解(全)

    本文介紹的是Xcode使用教程詳細(xì)講解,Xcode是一個款強(qiáng)大的IDE開發(fā)環(huán)境,就像你在寫Windows程序時需要VS2005一樣 需要要Xcode為你寫Mac程序提供環(huán)境
    2015-07-07
  • IOS改變UISearchBar中搜索框的高度

    IOS改變UISearchBar中搜索框的高度

    這篇文章主要介紹了IOS改變UISearchBar中搜索框的高度的相關(guān)資料,需要的朋友可以參考下
    2016-01-01
  • safari cookie設(shè)置中文失敗的解決方法

    safari cookie設(shè)置中文失敗的解決方法

    下面小編就為大家?guī)硪黄猻afari cookie設(shè)置中文失敗的解決方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-08-08

最新評論

宜州市| 紫金县| 思南县| 宝丰县| 平和县| 新邵县| 宝应县| 松溪县| 林州市| 曲沃县| 桦川县| 新沂市| 樟树市| 云和县| 阿巴嘎旗| 腾冲县| 乌拉特后旗| 河北省| 怀安县| 天全县| 信宜市| 江西省| 华亭县| 和顺县| 达日县| 青川县| 乐清市| 三原县| 饶河县| 临洮县| 无极县| 长寿区| 丘北县| 石柱| 郑州市| 徐汇区| 札达县| 中江县| 万山特区| 沈丘县| 阳山县|