VolumeInputKeyboardView

VolumeInputKeyboardView

自定义键盘类VolumeInputKeyboardView:

VolumeInputKeyboardView.h文件:

 #import <UIKit/UIKit.h>

 NS_ASSUME_NONNULL_BEGIN

 @interface VolumeInputKeyboardView : UIView
@property(weak, nonatomic) UITextField *m_textField;
@end NS_ASSUME_NONNULL_END

VolumeInputKeyboardView.m文件:

 switch  ::
#import <UIKit/UIKit.h> NS_ASSUME_NONNULL_BEGIN @interface VolumeInputKeyboardView : UIView
@property(weak, nonatomic) UITextField *m_textField;
@end NS_ASSUME_NONNULL_END switch ::
#import "VolumeInputKeyboardView.h"
#import "CommonFunc.h" @implementation VolumeInputKeyboardView - (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self createTopBar];
float width = (CGRectGetWidth(self.frame) - ) / ;
for(int i = ; i < ; ++i)
{
[self createKey:[NSString stringWithFormat:@"%d",i] posX:(width * ((i - ) % ) + ((i - ) % )) posY:( + * ((i - )/)) width:width height:];
}
[self createKey:@"" posX: posY:( + * ) width:(width * + ) height:];
[self createKey:@"←" posX:(width * + ) posY:( + * ) width:(width) height:];
[self createKey:@"+" posX:(width + ) * posY: width:width height:];
[self createKey:@"-" posX:(width + ) * posY:( + * ) width:(width) height:];
}
return self;
} - (void)createTopBar
{
UIView *topView = [UIView new];
[topView setBackgroundColor:[UIColor darkGrayColor]];
[self addSubview:topView];
topView.frame = CGRectMake(, , CGRectGetWidth(self.frame), ); UILabel *lbInfo = [[UILabel alloc]initWithFrame:CGRectMake(, , CGRectGetWidth(topView.frame) / * - , CGRectGetHeight(topView.frame))];
[topView addSubview:lbInfo];
[lbInfo setText:@"一些提示信息"];
[lbInfo setLineBreakMode:NSLineBreakByWordWrapping];
[lbInfo setNumberOfLines:];
[lbInfo setFont:[UIFont systemFontOfSize:]];
[lbInfo setTextColor:[UIColor whiteColor]]; UIButton *btnFinish = [[UIButton alloc]initWithFrame:CGRectMake(CGRectGetWidth(topView.frame) / * , , CGRectGetWidth(topView.frame) / , CGRectGetHeight(topView.frame))];
[topView addSubview:btnFinish];
[btnFinish setTitle:@"完成" forState:UIControlStateNormal];
[btnFinish setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[btnFinish setBackgroundImage:[CommonFunc imageWithColor:[UIColor orangeColor]] forState:UIControlStateHighlighted];
[btnFinish addTarget:self action:@selector(hideKeyboard) forControlEvents:UIControlEventTouchUpInside];
} - (void)createKey:(NSString *)title posX:(float)posX posY:(float)posY width:(float)width height:(float)height
{
UIButton *btnKey = [[UIButton alloc]initWithFrame:CGRectMake(posX, posY, width, height)];
[self addSubview:btnKey];
[btnKey addTarget:self action:@selector(buttonDidClicked:) forControlEvents:UIControlEventTouchUpInside];
[btnKey setTitle:title forState:UIControlStateNormal];
[btnKey setBackgroundColor:[UIColor lightGrayColor]];
[btnKey setBackgroundImage:[CommonFunc imageWithColor:[UIColor orangeColor]] forState:UIControlStateHighlighted];
} - (void)buttonDidClicked:(UIButton*)sender
{
self.m_textField.text = sender.titleLabel.text;
} -(void)layoutSubviews{
[super layoutSubviews]; } - (void)hideKeyboard
{
//隐藏键盘
[self.m_textField endEditing:YES];
}
@end

调用的地方:

 VolumeInputKeyboardView *volumeKeyboard = [[VolumeInputKeyboardView alloc]initWithFrame:CGRectMake(, , SCREEN_WIDTH, )];

     volumeKeyboard.m_textField = self.m_textField;
//设置输入框的键盘为自定义的键盘
self.textField.inputView = volumeKeyboard;

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

iOS自定义键盘的简单实现-LMLPHP

05-22 00:49