iOS开发系列之惯用自定义控件开发集—自定义数字键盘控件开发
iOS开发系列之常用自定义控件开发集—自定义数字键盘控件开发
在很多时候系统提供的数字键盘满足不了我们的需求,系统的数字键盘没有返回按钮在有些时候使用不是很方便所以我们需要自定义数字键盘,实现代码如下:
WHC_KeyBorad.h头文件如下:
//
// WHC_KeyBorad.h
// WHC_KeyBorad
//
// Created by 吴海超 on 15/3/23.
// Copyright (c) 2015年 吴海超. All rights reserved.
//
#import <UIKit/UIKit.h>
#define KWHC_KeyBorad_Exit_Id (10) //键盘删除id
#define KWHC_KeyBorad_Return_Id (11) //键盘返回id
#define KWHC_KeyBorad_Line_Color ([UIColor grayColor].CGColor) //键盘布局线的颜色
#define KWHC_KeyBorad_Button_Normal_Color ([UIColor whiteColor]) //键盘按钮背景正常文字颜色
#define KWHC_KeyBorad_Button_Selected_Color ([UIColor grayColor]) //键盘按钮选背景中文字颜色
#define KWHC_KeyBorad_BUtton_Title_Color ([UIColor blackColor]) //键盘按钮文字颜色
@interface WHC_KeyBorad : UIView
@property (nonatomic,strong) UITextField * textField; //关联的编辑框控件
@end
WHC_KeyBorad.m源文件如下:
//
// WHC_KeyBorad.m
// WHC_KeyBorad
//
// Created by 吴海超 on 15/3/23.
// Copyright (c) 2015年 吴海超. All rights reserved.
//
#import "WHC_KeyBorad.h"
@interface WHC_KeyBorad_Param : NSObject
@end
@implementation WHC_KeyBorad_Param
@end
#define KWHC_KeyBorad_Font_Size (30.0) //键盘按钮最大的字体大小
#define KWHC_KeyBorad_Line_Width (1.0) //键盘布局线宽
#define KWHC_Row_Button_Num (3) //键盘每行按钮个数
#define KWHC_Colum_Button_Num (4) //键盘没列按钮个数
@interface WHC_KeyBorad (){
CGFloat _buttonWidth; //存储键盘按钮宽度
CGFloat _buttonHeight; //存储键盘按钮高度
NSArray *_buttonTextArr; //存储键盘按钮文字数组
}
@end
@implementation WHC_KeyBorad
//键盘初始化
- (instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if(self != nil){
self.backgroundColor = [UIColor whiteColor];
_buttonWidth = (CGRectGetWidth(frame) - (KWHC_Row_Button_Num - 1) * KWHC_KeyBorad_Line_Width) / (CGFloat)KWHC_Row_Button_Num;
_buttonHeight = (CGRectGetHeight(frame) - (KWHC_Colum_Button_Num - 1) * KWHC_KeyBorad_Line_Width) / (CGFloat)KWHC_Colum_Button_Num;
_buttonTextArr = @[@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"0",@"×",@"返回"];
for (NSInteger i = 0; i < KWHC_Colum_Button_Num; i++) {
for (NSInteger j = 0; j < KWHC_Row_Button_Num; j++) {
UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.tag = j + i * (KWHC_Colum_Button_Num - 1);
btn.backgroundColor = [UIColor whiteColor];
btn.frame = CGRectMake(j * (_buttonWidth + KWHC_KeyBorad_Line_Width),
i * (_buttonHeight + KWHC_KeyBorad_Line_Width),
_buttonWidth, _buttonHeight);
btn.titleLabel.font = [UIFont boldSystemFontOfSize:KWHC_KeyBorad_Font_Size];
[btn setTitle:_buttonTextArr[btn.tag] forState:UIControlStateNormal];
[btn setTitleColor:KWHC_KeyBorad_BUtton_Title_Color forState:UIControlStateNormal];
[btn addTarget:self action:@selector(clickUPKeyBoradButton:) forControlEvents:UIControlEventTouchUpInside];
[btn addTarget:self action:@selector(clickDownKeyBoradButton:) forControlEvents:UIControlEventTouchDown];
[self addSubview:btn];
}
}
//注册屏幕旋转通知
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(screenChange:) name:UIDeviceOrientationDidChangeNotification object:nil];
}
return self;
}
//处理屏幕旋转通知
- (void)screenChange:(NSNotification*)notifiy{
_buttonWidth = (CGRectGetWidth([UIScreen mainScreen].bounds) - (KWHC_Row_Button_Num - 1) * KWHC_KeyBorad_Line_Width) / (CGFloat)KWHC_Row_Button_Num;
for (int i = 0; i < KWHC_Colum_Button_Num; i++) {
//根据屏幕状态动态计算键盘按钮宽度和高度
for (int j = 0; j < KWHC_Row_Button_Num; j++) {
((UIButton *)self.subviews[j + i * (KWHC_Colum_Button_Num - 1)]).frame =
CGRectMake(j * (_buttonWidth + KWHC_KeyBorad_Line_Width),
i * (_buttonHeight + KWHC_KeyBorad_Line_Width),
_buttonWidth, _buttonHeight);
}
}
}
//关联编辑框控件
- (void)setTextField:(UITextField *)textField{
_textField = textField;
self.textField.inputView = self;
}
#pragma mark - clickAction
//单击键盘按钮高亮背景
-(void)clickDownKeyBoradButton:(UIButton*)sender{
sender.backgroundColor = KWHC_KeyBorad_Button_Selected_Color;
}
//单击键盘按钮处理
- (void)clickUPKeyBoradButton:(UIButton*)sender{
[[UIDevice currentDevice] playInputClick];
sender.backgroundColor = KWHC_KeyBorad_Button_Normal_Color;
switch (sender.tag) {
case KWHC_KeyBorad_Exit_Id:{
NSString * strEdit = self.textField.text;
if(strEdit != nil && strEdit.length > 0){
self.textField.text = [strEdit substringToIndex:strEdit.length - 1];
}
}
break;
case KWHC_KeyBorad_Return_Id:
[self.textField resignFirstResponder];
break;
default:{
self.textField.text = [NSString stringWithFormat:@"%@%@",self.textField.text,sender.titleLabel.text];
}
break;
}
}
#pragma mark - drawRectBackUI
- (void)drawRect:(CGRect)rect {
// drawing keyBorad back UI
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, KWHC_KeyBorad_Line_Width);
CGContextSetStrokeColorWithColor(context, KWHC_KeyBorad_Line_Color);
CGContextMoveToPoint(context, 0.0, _buttonHeight);
for (int i = 0; i < KWHC_Colum_Button_Num - 1; i++) {
CGContextAddLineToPoint(context, CGRectGetWidth(rect), _buttonHeight * (i + 1) + i * KWHC_KeyBorad_Line_Width);
CGContextClosePath(context);
CGContextMoveToPoint(context, 0.0, _buttonHeight * (i + 2) + (i + 1) * KWHC_KeyBorad_Line_Width);
}
CGContextMoveToPoint(context, _buttonWidth, 0.0);
for (int i = 0; i < KWHC_Row_Button_Num - 1; i++) {
CGContextAddLineToPoint(context, _buttonWidth * (i + 1) + i * KWHC_KeyBorad_Line_Width, CGRectGetHeight(rect));
CGContextClosePath(context);
CGContextMoveToPoint(context, _buttonWidth * (i + 2) + (i + 1) * KWHC_KeyBorad_Line_Width, 0.0);
}
CGContextStrokePath(context);
}
//释放通知
- (void)dealloc
{
[[NSNotificationCenter defaultCenter]removeObserver:self];
}
@end
WHC_NumberKeyBoradDemo下载