【iOS自定义键盘及键盘切换】详解

【iOS自定义键盘】详解

实现效果展示:

【iOS自定义键盘及键盘切换】详解

一、实现的协议方法代码

 #import <UIKit/UIKit.h>

 //创建自定义键盘协议
@protocol XFG_KeyBoardDelegate <NSObject>
//创建协议方法
@required//必须执行的方法
- (void)numberKeyBoard:(NSInteger) number;
- (void)cancelKeyBoard;
- (void)finishKeyBoard;
- (void)periodKeyBoard;
- (void)changeKeyBoard;
@optional//不必须执行方法 @end @interface XFG_KeyBoard : UIView
{
@private//私有的协议方法
id<XFG_KeyBoardDelegate> _delegate;
} @property (nonatomic, strong) id<XFG_KeyBoardDelegate> delegate; - (id)initWithNumber:(NSNumber *)number;
@end

二、自定义键盘创建

 #import "XFG_KeyBoard.h"
#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define kScreenHeight [UIScreen mainScreen].bounds.size.height @implementation XFG_KeyBoard - (id)initWithNumber:(NSNumber *)number
{
self = [super init];
if (self) { self.backgroundColor = [UIColor greenColor];
self.frame = CGRectMake(, kScreenHeight - , kScreenHeight, );
if ([number isEqualToNumber:@]) { [self initKeyBoardNumber_1];
} else if ([number isEqualToNumber:@]){ [self initKeyBoardNumber_2];
} else if ([number isEqualToNumber:@]){ [self initKeyBoardNumber_3];
}
} return self;
} #pragma 键盘1自定义视图
- (void)initKeyBoardNumber_1
{
self.frame=CGRectMake(, kScreenHeight-, kScreenWidth, );
int space=;
for (int i=; i<; i++) {
NSString *str=[NSString stringWithFormat:@"%d",i+];
UIButton *button=[UIButton buttonWithType:UIButtonTypeSystem];
if (i<) {
button.frame=CGRectMake(i%*(kScreenWidth/)+space, i/*, kScreenWidth/-, );
}
else{
button.frame=CGRectMake(i%*(kScreenWidth/)+space, i/*+i/*space, kScreenWidth/-, );
}
button.backgroundColor=[UIColor whiteColor];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
button.titleLabel.font=[UIFont systemFontOfSize:];
[button setTitle:str forState:UIControlStateNormal];
button.tag=i+;
[button addTarget:self action:@selector(keyBoardAciont:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:button];
}
UIButton *dian=[UIButton buttonWithType:UIButtonTypeSystem];
dian.frame=CGRectMake(space,*+ , kScreenWidth/-, );
dian.backgroundColor=[UIColor whiteColor];
[dian setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
dian.titleLabel.font=[UIFont systemFontOfSize:];
[dian addTarget:self action:@selector(keyBoardAciont:) forControlEvents:UIControlEventTouchUpInside];
[dian setTitle:@"." forState:UIControlStateNormal];
dian.tag=;
[self addSubview:dian];
UIButton *ling=[UIButton buttonWithType:UIButtonTypeSystem];
ling.frame=CGRectMake(kScreenWidth/+*space,*+, kScreenWidth/-, );
ling.backgroundColor=[UIColor whiteColor];
[ling setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
ling.titleLabel.font=[UIFont systemFontOfSize:];
[ling setTitle:@"" forState:UIControlStateNormal];
ling.tag=;
[ling addTarget:self action:@selector(keyBoardAciont:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:ling]; UIButton *computer=[UIButton buttonWithType:UIButtonTypeSystem];
computer.frame=CGRectMake(kScreenWidth/*+space,*+, kScreenWidth/-, );
computer.backgroundColor=[UIColor whiteColor];
[computer setTitle:@"系统键盘" forState:UIControlStateNormal];
computer.tag=;
[computer addTarget:self action:@selector(keyBoardAciont:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:computer]; UIButton *delete=[UIButton buttonWithType:UIButtonTypeSystem];
delete.frame=CGRectMake(kScreenWidth/*+space,, kScreenWidth/-, );
[delete addTarget:self action:@selector(keyBoardAciont:) forControlEvents:UIControlEventTouchUpInside];
delete.tag=; UIImageView *deleteImage=[[UIImageView alloc]initWithFrame:CGRectMake((kScreenWidth/- - ) * 1.0 / , , , )];
deleteImage.image=[UIImage imageNamed:@"goumai_03"];
[delete addSubview:deleteImage]; // [delete setBackgroundImage:[UIImage imageNamed:@"goumai_03"] forState:UIControlStateNormal];
[self addSubview:delete]; UIButton *confirm=[UIButton buttonWithType:UIButtonTypeSystem];
confirm.frame=CGRectMake(kScreenWidth/*+space,*, kScreenWidth/-, );
confirm.backgroundColor=[UIColor blueColor];
[confirm setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
confirm.titleLabel.font=[UIFont systemFontOfSize:];
[confirm setTitle:@"确 定" forState:UIControlStateNormal];
[confirm addTarget:self action:@selector(keyBoardAciont:) forControlEvents:UIControlEventTouchUpInside];
confirm.tag=;
[self addSubview:confirm];
} #pragma 键盘2自定义视图
- (void)initKeyBoardNumber_2
{
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(, , kScreenWidth - , )]; button.backgroundColor = [UIColor grayColor];
button.titleLabel.font = [UIFont systemFontOfSize:];
[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[button addTarget:self action:@selector(keyBoardAciont:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:button];
button.tag = ;
[button setTitle:@"" forState:UIControlStateNormal]; UIButton *cancel = [[UIButton alloc] initWithFrame:CGRectMake(button.bounds.origin.x + button.bounds.size.width, , , )]; cancel.backgroundColor = [UIColor yellowColor];
cancel.titleLabel.font = [UIFont systemFontOfSize:];
[cancel setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[cancel addTarget:self action:@selector(keyBoardAciont:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:cancel];
cancel.tag = ;
[cancel setTitle:@"删 除" forState:UIControlStateNormal];
} #pragma 键盘3自定义视图
- (void)initKeyBoardNumber_3
{
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(, , kScreenWidth - , )]; button.backgroundColor = [UIColor purpleColor];
button.titleLabel.font = [UIFont systemFontOfSize:];
[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[button addTarget:self action:@selector(keyBoardAciont:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:button];
button.tag = ;
[button setTitle:@"" forState:UIControlStateNormal]; UIButton *cancel = [[UIButton alloc] initWithFrame:CGRectMake(button.bounds.origin.x + button.bounds.size.width, , , )]; cancel.backgroundColor = [UIColor yellowColor];
cancel.titleLabel.font = [UIFont systemFontOfSize:];
[cancel setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[cancel addTarget:self action:@selector(keyBoardAciont:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:cancel];
cancel.tag = ;
[cancel setTitle:@"删 除" forState:UIControlStateNormal];
} #pragma 键盘点击按钮事件
- (void)keyBoardAciont:(UIButton *)sender
{ UIButton* btn = (UIButton*)sender; NSInteger number = btn.tag; // no delegate, print log info
if (nil == _delegate) {
NSLog(@"button tag [%ld]",(long)number);
return;
} if (number <= && number >= ) {
[_delegate numberKeyBoard:number];
return;
} if ( == number) {
[_delegate cancelKeyBoard];
return;
}
if ( == number) {
[_delegate periodKeyBoard];
return;
}
if ( == number) {
[_delegate changeKeyBoard];
return;
} if ( == number) {
[_delegate finishKeyBoard];
return;
} } @end

三、自定义键盘运用

 #import "ViewController.h"
#import "XFG_KeyBoard.h"
#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define kScreenHeight [UIScreen mainScreen].bounds.size.height @interface ViewController ()<UITextFieldDelegate, XFG_KeyBoardDelegate> @property (nonatomic, strong) XFG_KeyBoard *xfg_keyboard;
@property (nonatomic, strong) UITextField *textField;
@end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.title = @"小飞哥键盘";
self.textField = [[UITextField alloc] initWithFrame:CGRectMake(, , kScreenWidth - , )];
self.textField.delegate = self;
self.textField.backgroundColor = [UIColor greenColor];
self.textField.placeholder = @"(默认系统键盘)"; [self.view addSubview:self.textField]; UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hidenKeyBoard)];
[self.view addGestureRecognizer:tap]; UISegmentedControl *segment = [[UISegmentedControl alloc] initWithItems:@[@"键盘1",@"键盘2",@"键盘3",@"系统键盘"]];
segment.frame = CGRectMake(, , kScreenWidth, );
[segment addTarget:self action:@selector(keyBoardTypeAction:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:segment]; } - (void)keyBoardTypeAction:(id )sender
{
UISegmentedControl *seg = (UISegmentedControl *)sender;
NSLog(@"intage = %ld", (long)seg.selectedSegmentIndex); if (seg.selectedSegmentIndex == ) {
self.xfg_keyboard = [[XFG_KeyBoard alloc] initWithNumber:@];
self.textField.inputView = self.xfg_keyboard;
self.xfg_keyboard.delegate = self;
[self.textField reloadInputViews];
}
if (seg.selectedSegmentIndex == ) {
self.xfg_keyboard = [[XFG_KeyBoard alloc] initWithNumber:@];
self.textField.inputView = self.xfg_keyboard;
self.xfg_keyboard.delegate = self;
[self.textField reloadInputViews]; } if (seg.selectedSegmentIndex == ) {
self.xfg_keyboard = [[XFG_KeyBoard alloc] initWithNumber:@];
self.textField.inputView = self.xfg_keyboard;
self.xfg_keyboard.delegate = self;
[self.textField reloadInputViews];
} if (seg.selectedSegmentIndex == ) {
self.textField.inputView = nil;
[self.textField reloadInputViews];
} } - (void)hidenKeyBoard
{
NSLog(@"键盘隐藏");
[self.textField resignFirstResponder];
} - (void)textFieldDidBeginEditing:(UITextField *)textField
{
NSLog(@"显示键盘");
} - (void)numberKeyBoard:(NSInteger)number
{
NSString *str = self.textField.text;
self.textField.text = [NSString stringWithFormat:@"%@%ld",str,(long)number];
} - (void)cancelKeyBoard
{ NSMutableString *muStr = [[NSMutableString alloc] initWithString:self.textField.text];
if (muStr.length <= ) {
return;
}
[muStr deleteCharactersInRange:NSMakeRange([muStr length] - , )];
self.textField.text = muStr;
} #pragma 输入点
-(void)periodKeyBoard{ if ([self.textField.text isEqualToString:@""]) {
return;
} //判断当前时候存在一个点
if ([self.textField.text rangeOfString:@"."].location == NSNotFound) {
//输入中没有点
NSMutableString *mutableString=[[NSMutableString alloc]initWithFormat:@"%@%@",self.textField.text,@"."];
self.textField.text=mutableString;
}
} -(void)changeKeyBoard{ self.textField.inputView = nil;
[self.textField reloadInputViews];
} -(void)finishKeyBoard{
[self.view endEditing:YES];
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end
上一篇:imread() not working in OpenCV 2.4.11 Debug mode


下一篇:Django REST framework基础:解析器和渲染器