iOS自定义键盘的简单实现

自定义键盘类VolumeInputKeyboardView:

VolumeInputKeyboardView.h文件:

1 #import <UIKit/UIKit.h>
2 
3 NS_ASSUME_NONNULL_BEGIN
4 
5 @interface VolumeInputKeyboardView : UIView
6 @property(weak, nonatomic) UITextField *m_textField;
7 @end
8 
9 NS_ASSUME_NONNULL_END

VolumeInputKeyboardView.m文件:

 1 switch  17:22:32
 2 #import <UIKit/UIKit.h>
 3 
 4 NS_ASSUME_NONNULL_BEGIN
 5 
 6 @interface VolumeInputKeyboardView : UIView
 7 @property(weak, nonatomic) UITextField *m_textField;
 8 @end
 9 
10 NS_ASSUME_NONNULL_END
11 
12 switch  17:24:13
13 #import "VolumeInputKeyboardView.h"
14 #import "CommonFunc.h"
15 
16 @implementation VolumeInputKeyboardView
17 
18 
19 
20 - (instancetype)initWithFrame:(CGRect)frame
21 {
22     self = [super initWithFrame:frame];
23     if (self) {
24         [self createTopBar];
25         float width = (CGRectGetWidth(self.frame) - 3) / 4;
26         for(int i = 1; i < 10; ++i)
27         {
28             [self createKey:[NSString stringWithFormat:@"%d",i] posX:(width * ((i - 1) % 3) + ((i - 1) % 3)) posY:(44 + 45 * ((i - 1)/3)) width:width height:44];
29         }
30         [self createKey:@"0" posX:0 posY:(44 + 45 * 3) width:(width * 2 + 1) height:44];
31         [self createKey:@"" posX:(width * 2 + 2) posY:(44 + 45 * 3) width:(width) height:44];
32         [self createKey:@"+" posX:(width + 1) * 3 posY:44 width:width height:89];
33         [self createKey:@"-" posX:(width + 1) * 3 posY:(44 + 45 * 2) width:(width) height:89];
34     }
35     return self;
36 }
37 
38 - (void)createTopBar
39 {
40     UIView *topView = [UIView new];
41     [topView setBackgroundColor:[UIColor darkGrayColor]];
42     [self addSubview:topView];
43     topView.frame = CGRectMake(0, 0, CGRectGetWidth(self.frame), 44);
44     
45     UILabel *lbInfo = [[UILabel alloc]initWithFrame:CGRectMake(5, 0, CGRectGetWidth(topView.frame) / 4 * 3 - 5, CGRectGetHeight(topView.frame))];
46     [topView addSubview:lbInfo];
47     [lbInfo setText:@"一些提示信息"];
48     [lbInfo setLineBreakMode:NSLineBreakByWordWrapping];
49     [lbInfo setNumberOfLines:0];
50     [lbInfo setFont:[UIFont systemFontOfSize:14]];
51     [lbInfo setTextColor:[UIColor whiteColor]];
52     
53     UIButton *btnFinish = [[UIButton alloc]initWithFrame:CGRectMake(CGRectGetWidth(topView.frame) / 4 * 3, 0, CGRectGetWidth(topView.frame) / 4 , CGRectGetHeight(topView.frame))];
54     [topView addSubview:btnFinish];
55     [btnFinish setTitle:@"完成" forState:UIControlStateNormal];
56     [btnFinish setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
57     [btnFinish setBackgroundImage:[CommonFunc imageWithColor:[UIColor orangeColor]] forState:UIControlStateHighlighted];
58     [btnFinish addTarget:self action:@selector(hideKeyboard) forControlEvents:UIControlEventTouchUpInside];
59 }
60 
61 - (void)createKey:(NSString *)title posX:(float)posX posY:(float)posY width:(float)width height:(float)height
62 {
63     UIButton *btnKey = [[UIButton alloc]initWithFrame:CGRectMake(posX, posY, width, height)];
64     [self addSubview:btnKey];
65     [btnKey addTarget:self action:@selector(buttonDidClicked:) forControlEvents:UIControlEventTouchUpInside];
66     [btnKey setTitle:title forState:UIControlStateNormal];
67     [btnKey setBackgroundColor:[UIColor lightGrayColor]];
68     [btnKey setBackgroundImage:[CommonFunc imageWithColor:[UIColor orangeColor]] forState:UIControlStateHighlighted];
69 }
70 
71 - (void)buttonDidClicked:(UIButton*)sender
72 {
73     self.m_textField.text = sender.titleLabel.text;
74 }
75 
76 -(void)layoutSubviews{
77     [super layoutSubviews];
78     
79 }
80 
81 - (void)hideKeyboard
82 {
83     //隐藏键盘
84     [self.m_textField endEditing:YES];
85 }
86 @end

调用的地方:

1 VolumeInputKeyboardView *volumeKeyboard = [[VolumeInputKeyboardView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 224)];
2  
3     volumeKeyboard.m_textField = self.m_textField;
4     //设置输入框的键盘为自定义的键盘
5     self.textField.inputView = volumeKeyboard;

最终实现的键盘效果如下:

猜你喜欢

转载自www.cnblogs.com/luoluosha/p/11687077.html