iOS自定义数字键盘

Posted 开发工程师

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS自定义数字键盘相关的知识,希望对你有一定的参考价值。

自定义键盘实际就是设置UITextField的inputView属性,首先我们要做的是自定义一个UIView,实现键盘的样式。

自定义View代码如下:

 1 #import <UIKit/UIKit.h>
 2 //创建自定义键盘协议
 3 @protocol My_KeyBoardDelegate <NSObject>
 4 //创建协议方法
 5 @required//必须执行的方法
 6 - (void)numberKeyBoard:(NSInteger) number;
 7 - (void)cancelKeyBoard;
 8 - (void)finishKeyBoard;
 9 - (void)periodKeyBoard;
10 - (void)changeKeyBoard;
11 @optional//不必须执行方法
12 
13 @end
14 
15 @interface My_KeyBoardView : UIView
16 {
17 @private//私有的协议方法
18     id<My_KeyBoardDelegate> _delegate;
19 }
20 @property (nonatomic, strong) id<My_KeyBoardDelegate> delegate;
21 
22 - (id)initWithNumber:(NSNumber *)number;
23 @end

 

  1 #import "My_KeyBoardView.h"
  2 #define kScreenWidth   [UIScreen mainScreen].bounds.size.width
  3 #define kScreenHeight  [UIScreen mainScreen].bounds.size.height
  4 
  5 @implementation My_KeyBoardView
  6 
  7 
  8 - (id)initWithNumber:(NSNumber *)number;
  9 {
 10     self = [super init];
 11     if (self) {
 12         
 13         self.backgroundColor = [UIColor redColor];
 14         self.frame = CGRectMake(0, kScreenHeight - 150, kScreenHeight, 150);
 15         
 16             [self initKeyBoardNumber_1];
 17     }
 18     return self;
 19 }
 20 - (void)initKeyBoardNumber_1
 21 {
 22     self.frame=CGRectMake(0, kScreenHeight-243, kScreenWidth, 243);
 23     int space=1;
 24     for (int i=0; i<9; i++) {
 25         NSString *str=[NSString stringWithFormat:@"%d",i+1];
 26         UIButton *button=[UIButton buttonWithType:UIButtonTypeSystem];
 27         if (i<3) {
 28             button.frame=CGRectMake(i%3*(kScreenWidth/4)+space, i/3*61, kScreenWidth/4-1, 60);
 29         }
 30         else{
 31             button.frame=CGRectMake(i%3*(kScreenWidth/4)+space, i/3*60+i/3*space, kScreenWidth/4-1, 60);
 32         }
 33         button.backgroundColor=[UIColor whiteColor];
 34         [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
 35         button.titleLabel.font=[UIFont systemFontOfSize:24];
 36         [button setTitle:str forState:UIControlStateNormal];
 37         button.tag=i+1;
 38         [button addTarget:self action:@selector(keyBoardAciont:) forControlEvents:UIControlEventTouchUpInside];
 39         [self addSubview:button];
 40     }
 41     UIButton *dian=[UIButton buttonWithType:UIButtonTypeSystem];
 42     dian.frame=CGRectMake(space,60*3+3 , kScreenWidth/4-1, 60);
 43     dian.backgroundColor=[UIColor whiteColor];
 44     [dian setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
 45     dian.titleLabel.font=[UIFont systemFontOfSize:24];
 46     [dian addTarget:self action:@selector(keyBoardAciont:) forControlEvents:UIControlEventTouchUpInside];
 47     [dian setTitle:@"." forState:UIControlStateNormal];
 48     dian.tag=11;
 49     [self addSubview:dian];
 50     UIButton *ling=[UIButton buttonWithType:UIButtonTypeSystem];
 51     ling.frame=CGRectMake(kScreenWidth/4+1*space,60*3+3, kScreenWidth/4-1, 60);
 52     ling.backgroundColor=[UIColor whiteColor];
 53     [ling setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
 54     ling.titleLabel.font=[UIFont systemFontOfSize:24];
 55     [ling setTitle:@"0" forState:UIControlStateNormal];
 56     ling.tag=0;
 57     [ling addTarget:self action:@selector(keyBoardAciont:) forControlEvents:UIControlEventTouchUpInside];
 58     [self addSubview:ling];
 59     
 60     UIButton *computer=[UIButton buttonWithType:UIButtonTypeSystem];
 61     computer.frame=CGRectMake(kScreenWidth/4*2+space,60*3+3, kScreenWidth/4-1, 60);
 62     computer.backgroundColor=[UIColor whiteColor];
 63     [computer setTitle:@"系统键盘" forState:UIControlStateNormal];
 64     computer.tag=12;
 65     [computer addTarget:self action:@selector(keyBoardAciont:) forControlEvents:UIControlEventTouchUpInside];
 66     [self addSubview:computer];
 67     
 68     UIButton *delete=[UIButton buttonWithType:UIButtonTypeSystem];
 69     delete.frame=CGRectMake(kScreenWidth/4*3+space,1, kScreenWidth/4-1, 122);
 70     [delete addTarget:self action:@selector(keyBoardAciont:) forControlEvents:UIControlEventTouchUpInside];
 71     delete.tag=10;
 72     
 73     UIImageView *deleteImage=[[UIImageView alloc]initWithFrame:CGRectMake((kScreenWidth/4-1 - 28) * 1.0 / 2, 50, 28, 20)];
 74     deleteImage.image=[UIImage imageNamed:@"goumai_03"];
 75     [delete addSubview:deleteImage];
 76     
 77     // [delete setBackgroundImage:[UIImage imageNamed:@"goumai_03"] forState:UIControlStateNormal];
 78     [self addSubview:delete];
 79     
 80     UIButton *confirm=[UIButton buttonWithType:UIButtonTypeSystem];
 81     confirm.frame=CGRectMake(kScreenWidth/4*3+space,61*2, kScreenWidth/4-1, 122);
 82     confirm.backgroundColor=[UIColor blueColor];
 83     [confirm setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
 84     confirm.titleLabel.font=[UIFont systemFontOfSize:20];
 85     [confirm setTitle:@"确 定" forState:UIControlStateNormal];
 86     [confirm addTarget:self action:@selector(keyBoardAciont:) forControlEvents:UIControlEventTouchUpInside];
 87     confirm.tag=13;
 88     [self addSubview:confirm];
 89 }
 90 
 91 #pragma 键盘点击按钮事件
 92 - (void)keyBoardAciont:(UIButton *)sender
 93 {
 94     UIButton* btn = (UIButton*)sender;
 95     NSInteger number = btn.tag;
 96     // no delegate, print log info
 97     if (nil == _delegate) {
 98         NSLog(@"button tag [%ld]",(long)number);
 99         return;
100     }
101     
102     if (number <= 9 && number >= 0) {
103         [_delegate numberKeyBoard:number];
104         return;
105     }
106     
107     if (10 == number) {
108         [_delegate cancelKeyBoard];
109         return;
110     }
111     if (11 == number) {
112         [_delegate periodKeyBoard];
113         return;
114     }
115     if (12 == number) {
116         [_delegate changeKeyBoard];
117         return;
118     }
119     
120     if (13 == number) {
121         [_delegate finishKeyBoard];
122         return;
123     }
124     
125 }
126 @end

接下来是调用View并实现代理方法:

 1 #define kScreenWidth   [UIScreen mainScreen].bounds.size.width
 2 #define kScreenHeight  [UIScreen mainScreen].bounds.size.height
 3 
 4 #import "ViewController.h"
 5 #import "My_KeyBoardView.h"
 6 @interface ViewController ()<My_KeyBoardDelegate,UITextFieldDelegate>
 7 @property (nonatomic, strong) My_KeyBoardView *my_keyboard;
 8 @property (nonatomic, strong) UITextField *textField;
 9 @end
10 
11 @implementation ViewController
12 
13 - (void)viewDidLoad {
14     [super viewDidLoad];
15     // Do any additional setup after loading the view, typically from a nib.
16     self.textField = [[UITextField alloc] initWithFrame:CGRectMake(50, 168, kScreenWidth - 100, 50)];
17     self.textField.delegate = self;
18     self.textField.backgroundColor = [UIColor greenColor];
19     self.textField.placeholder = @"(默认系统键盘)";
20     
21     [self.view addSubview:self.textField];
22     
23     UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hidenKeyBoard)];
24     [self.view addGestureRecognizer:tap];
25     
26     [self keyBoardTypeAction];
27 }
28 - (void)keyBoardTypeAction
29 {
30 
31         self.my_keyboard = [[My_KeyBoardView alloc] initWithNumber:@1];
32         self.textField.inputView = self.my_keyboard;
33         self.my_keyboard.delegate = self;
34         [self.textField reloadInputViews];
35 }
36 - (void)hidenKeyBoard
37 {
38     NSLog(@"键盘隐藏");
39     [self.textField resignFirstResponder];
40 }
41 
42 - (void)textFieldDidBeginEditing:(UITextField *)textField
43 {
44     NSLog(@"显示键盘");
45 }
46 
47 - (void)numberKeyBoard:(NSInteger)number
48 {
49     NSString *str = self.textField.text;
50     self.textField.text = [NSString stringWithFormat:@"%@%ld",str,(long)number];
51 }
52 
53 - (void)cancelKeyBoard
54 {
55     
56     NSMutableString *muStr = [[NSMutableString alloc] initWithString:self.textField.text];
57     if (muStr.length <= 0) {
58         return;
59     }
60     [muStr deleteCharactersInRange:NSMakeRange([muStr length] - 1, 1)];
61     self.textField.text = muStr;
62 }
63 
64 #pragma 输入点
65 -(void)periodKeyBoard{
66     
67     if ([self.textField.text isEqualToString:@""]) {
68         return;
69     }
70     
71     //判断当前时候存在一个点
72     if ([self.textField.text rangeOfString:@"."].location == NSNotFound) {
73         //输入中没有点
74         NSMutableString  *mutableString=[[NSMutableString alloc]initWithFormat:@"%@%@",self.textField.text,@"."];
75         self.textField.text=mutableString;
76     }
77 }
78 -(void)changeKeyBoard{
79     self.textField.inputView = nil;
80     [self.textField reloadInputViews];
81 }
82 
83 -(void)finishKeyBoard{
84     [self.view endEditing:YES];
85 }
86 
87 @end

 

实现的效果:

以上是关于iOS自定义数字键盘的主要内容,如果未能解决你的问题,请参考以下文章

iOS应用程序中的自定义数字键盘问题

在 iOS 中有一个自定义数字键盘来拨打号码

带有自定义 UI 按钮的 iOS 键盘的辅助功能选项卡顺序

iPad应用程序的自定义数字键盘

ios添加自定义inputView

在 iPhone 小键盘 iOS9 上方添加自定义按钮