自定义键盘

时间:2018-07-05 08:17:26
【文件属性】:

文件名称:自定义键盘

文件大小:6KB

文件格式:ZIP

更新时间:2018-07-05 08:17:26

ios

// // BGKeyBoard.h // BGKeyBoard // // Created by SH-zhangketao on 15/6/11. // Copyright (c) 2015年 SH-zhangketao. All rights reserved. // #import @class BGKeyBoard; @protocol BGKeyBoardDelegate @required - (void)finished; @optional - (void)calculatorInputView:(BGKeyBoard *)inputView didTapKey:(NSString *)key; - (void)calculatorInputViewDidTapBackspace:(BGKeyBoard *)calculatorInputView; @end @interface BGKeyBoard : UIView @property (weak, nonatomic) id delegate; /**----------------------------------------------------------------------------- * @name Customizing colors * ----------------------------------------------------------------------------- */ @property (strong, nonatomic) UIColor *buttonTitleColor; @property (strong, nonatomic) UIFont *buttonTitleFont; @property (strong, nonatomic) UIColor *buttonHighlightedColor; @property (strong, nonatomic) UIColor *numberButtonBackgroundColor; @property (strong, nonatomic) UIColor *numberButtonBorderColor; @end // // BGKeyBoard.m // BGKeyBoard // // Created by SH-zhangketao on 15/6/11. // Copyright (c) 2015年 SH-zhangketao. All rights reserved. // #import "BGKeyBoard.h" #define kBtnw 65 //按键宽 #define kBtnh 45 //按键高 #define kTotalloc 4 //键盘列数 #define kScreenSize [UIScreen mainScreen].applicationFrame.size //界面尺寸 #define kMarginx (kScreenSize.width - kTotalloc * kBtnw) / (kTotalloc + 1) //横向间隔 #define kMarginy (kScreenSize.width - kTotalloc * kBtnw) / (kTotalloc + 1) - 5 //纵向间隔 @interface BGKeyBoard () @property (strong, nonatomic) NSMutableArray *numbers;//数字集合 @property (strong, nonatomic) NSMutableArray *btns; //按键集合 @end @implementation BGKeyBoard - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { self.backgroundColor = [UIColor whiteColor]; _btns = [[NSMutableArray alloc] init]; _numbers = [[NSMutableArray alloc] init]; NSArray *array = @[@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9"]; //随机获取一个数字序列 array = [array sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) { return (arc4random() % 3) - 1; }]; [_numbers addObjectsFromArray:array]; [_numbers addObject:@"."]; [_numbers addObject:@"删除"]; [_numbers addObject:@"数字"]; [_numbers addObject:@"字母"]; [_numbers addObject:@"符号"]; [_numbers addObject:@"确定"]; [self createButton]; } return self; } /** 回执键盘 */ - (void)createButton { for (NSInteger index = 0; index < _numbers.count; index++) { UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; [btn setTitle:_numbers[index] forState:UIControlStateNormal]; btn.backgroundColor = [UIColor lightGrayColor]; NSInteger row = index / kTotalloc;//行号 NSInteger loc = index % kTotalloc;//列号 CGFloat btnx = kMarginx + (kMarginx + kBtnw) * loc; CGFloat btny = kMarginy + (kMarginy + kBtnh) * row; btn.frame = CGRectMake(btnx, btny, kBtnw, kBtnh); [self setBtnAppearance:btn]; btn.tag = index; if (index == 11) { [btn addTarget:self action:@selector(userDidTapBackspace:) forControlEvents:UIControlEventTouchUpInside]; } else if (index > 11) { [btn addTarget:self action:@selector(dsfasdf:) forControlEvents:UIControlEventTouchUpInside]; } else { [btn addTarget:self action:@selector(userDidTapKey:) forControlEvents:UIControlEventTouchUpInside]; } if (index == 15) { btn.backgroundColor = [UIColor colorWithRed:0/255.0 green:170/255.0 blue:245/255.0 alpha:1.0]; } btn.showsTouchWhenHighlighted = YES; [self addSubview:btn]; [_btns addObject:btn]; } } /** 按键外观设置 */ - (void)setBtnAppearance:(UIButton *)btn { btn.layer.cornerRadius = 5.0; btn.clipsToBounds = YES; btn.layer.borderWidth = 0.25f; } /** 删除按钮事件 */ - (void)userDidTapBackspace:(UIButton *)sender { [[UIDevice currentDevice] playInputClick]; if ([self.delegate respondsToSelector:@selector(calculatorInputViewDidTapBackspace:)]) { [self.delegate calculatorInputViewDidTapBackspace:self]; } } /** 按键输入事件 */ - (void)userDidTapKey:(UIButton *)sender { [[UIDevice currentDevice] playInputClick]; if ([self.delegate respondsToSelector:@selector(calculatorInputView:didTapKey:)]) { [self.delegate calculatorInputView:self didTapKey:sender.titleLabel.text]; } } /** 输入完成 */ - (void)dsfasdf:(UIButton *)sender { [[UIDevice currentDevice] playInputClick]; if (sender.tag == 15) { if ([self.delegate respondsToSelector:@selector(finished)]) { [self.delegate finished]; } } } #pragma mark - UIInputViewAudioFeedback - (BOOL)enableInputClicksWhenVisible { return YES; } #pragma mark - Helpers - (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size { UIGraphicsBeginImageContextWithOptions(size, NO, 0); CGContextRef context = UIGraphicsGetCurrentContext(); [color set]; CGContextFillRect(context, CGRectMake(0, 0, size.width, size.height)); UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return image; } #pragma mark - Properties /** 按键标题颜色设置 */ - (void)setButtonTitleColor:(UIColor *)buttonTitleColor { _buttonTitleColor = buttonTitleColor; for (UIButton *numberButton in self.btns) { [numberButton setTitleColor:buttonTitleColor forState:UIControlStateNormal]; } } /** 按键标题字体设置 */ - (void)setButtonTitleFont:(UIFont *)buttonTitleFont { _buttonTitleFont = buttonTitleFont; for (UIButton *numberButton in self.btns) { numberButton.titleLabel.font = buttonTitleFont; } } /** 按键高亮颜色设置 */ - (void)setButtonHighlightedColor:(UIColor *)buttonHighlightedColor { _buttonHighlightedColor = buttonHighlightedColor; for (UIButton *numberButton in self.btns) { [numberButton setBackgroundImage:[self imageWithColor:buttonHighlightedColor size:CGSizeMake(50, 50)] forState:UIControlStateHighlighted]; } } /** 按键背景颜色设置 */ - (void)setNumberButtonBackgroundColor:(UIColor *)numberButtonBackgroundColor { _numberButtonBackgroundColor = numberButtonBackgroundColor; for (UIButton *numberButton in self.btns) { numberButton.backgroundColor = numberButtonBackgroundColor; } } /** 按键边界颜色设置 */ - (void)setNumberButtonBorderColor:(UIColor *)numberButtonBorderColor { _numberButtonBorderColor = numberButtonBorderColor; for (UIButton *numberButton in self.btns) { numberButton.layer.borderColor = numberButtonBorderColor.CGColor; } } @end


【文件预览】:
BGKeyBoard
----BGKeyBoard.m(6KB)
----BGInputTextField.h(224B)
----BGInputTextField.m(2KB)
----BGKeyBoard.h(1KB)

网友评论