iOSカスタムIDカードキーボード
本論文の例では、iOSカスタム身分証キーボードの具体的なコードを共有します。参考にしてください。具体的な内容は以下の通りです。
項目の中に身分証が必要な入力ボックスがあります。自分の入力で切り替えるのは面倒くさいです。(最後の方がXを持っているなら)、身分証をカスタマイズしてキーボードを入力します。
カスタムキーボードのキー: self.text Field.inputView=[カスタムview]
長い押しをサポートします。ずっと削除します。
デモ住所
カスタムを開始
1.UID Viewから一体化したビューを作成する(NLIDKeyBoard)
NLIDKeyBoard.h
項目の中に身分証が必要な入力ボックスがあります。自分の入力で切り替えるのは面倒くさいです。(最後の方がXを持っているなら)、身分証をカスタマイズしてキーボードを入力します。
カスタムキーボードのキー: self.text Field.inputView=[カスタムview]
長い押しをサポートします。ずっと削除します。
デモ住所
カスタムを開始
1.UID Viewから一体化したビューを作成する(NLIDKeyBoard)
NLIDKeyBoard.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
NLIDKeyBoard.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
controlerで使用する
//
// 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
以上が本文の全部です。皆さんの勉強に役に立つように、私たちを応援してください。