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

iOS自定義身份證鍵盤(pán)

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

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

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

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

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

demo地址

開(kāi)始自定義

1. 創(chuàng)建一個(gè)集成自UIView的視圖 (NYLIDKeyBoard)

NYLIDKeyBoard.h

//
// NYLIDKeyBoard.h
// lqz
//
// Created by 聶銀龍 on 2017/9/7.
// Copyright © 2017年 lqz. All rights reserved.
// 身份證鍵盤(pán)
 
#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)來(lái)確定 子控件的坐標(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(@"自定義鍵盤(pán)按鈕方法===== %@", 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è)置自定義鍵盤(pán)
 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];
}
 
 
// 身份證鍵盤(pán)
- (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實(shí)現(xiàn)動(dòng)態(tài)的開(kāi)屏廣告示例代碼

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    IOS改變UISearchBar中搜索框的高度

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

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

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

最新評(píng)論

江华| 洞口县| 湖北省| 尚义县| 卢湾区| 郧西县| 张家港市| 望江县| 梨树县| 夏河县| 延寿县| 柞水县| 门头沟区| 无锡市| 额尔古纳市| 抚顺县| 肇东市| 涞源县| 南部县| 巨野县| 黄平县| 建宁县| 门头沟区| 万源市| 乳源| 抚松县| 睢宁县| 郸城县| 汪清县| 通海县| 南城县| 神池县| 屯留县| 陈巴尔虎旗| 洛宁县| 安仁县| 子洲县| 金秀| 峨眉山市| 宁武县| 麻栗坡县|