自定義資料鍵盤
* .h 檔案
@protocol SLNumericKeyboardDelegate <NSObject>
- (void) numberKeyboardInput:(NSInteger) number;
- (void) numberKeyboardBackspaceTag:(NSInteger)tag;
- (void) changeKeyboardType;
@end
@interface SLNumericKeyboard : UIView {
NSArray *arrLetter;
}
@property (nonatomic,assign) id<SLNumericKeyboardDelegate> delegate;
@property (nonatomic,retain) UILabel *customLbl; //!< 自定義的一個鍵
@end
* .m 檔案
#import "SLNumericKeyboard.h"
#define kLineWidth .5
#define kNumFont [UIFont systemFontOfSize:28]
#define SCREENWIDTH [UIScreen mainScreen].bounds.size.width
@implementation SLNumericKeyboard
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.bounds = CGRectMake(, , SCREENWIDTH, );
arrLetter = [NSArray arrayWithObjects:@"ABC",@"DEF",@"GHI",@"JKL",@"MNO",@"PQRS",@"TUV",@"WXYZ", nil];
for (int i=; i<; i++) {
for (int j=; j<; j++) {
UIButton *button = [self creatButtonWithX:i Y:j];
[self addSubview:button];
}
}
UIColor *color = [UIColor lightGrayColor];// [UIColor colorWithRed:188/255.0 green:192/255.0 blue:199/255.0 alpha:1];
// UIView *line0 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, .6)];
// line0.backgroundColor = color;
// [self addSubview:line0];
UIView *line1 = [[UIView alloc] initWithFrame:CGRectMake(SCREENWIDTH / , , kLineWidth, )];
line1.backgroundColor = color;
[self addSubview:line1];
UIView *line2 = [[UIView alloc] initWithFrame:CGRectMake(SCREENWIDTH/*+, , kLineWidth, )];
line2.backgroundColor = color;
[self addSubview:line2];
for (int i=; i<; i++) {
UIView *line = [[UIView alloc] initWithFrame:CGRectMake(, *(i+), SCREENWIDTH, kLineWidth)];
line.backgroundColor = color;
[self addSubview:line];
}
}
return self;
}
-(UIButton *)creatButtonWithX:(NSInteger)x Y:(NSInteger)y {
UIButton *button;
button.backgroundColor = [UIColor redColor];
CGFloat frameX;
CGFloat frameW = SCREENWIDTH / ;
switch (y) {
case :
frameX = ;
break;
case :
frameX = frameW + ;
break;
case :
frameX = frameW * + ;
break;
default:
break;
}
CGFloat frameY = *x;
button = [[UIButton alloc] initWithFrame:CGRectMake(frameX, frameY, frameW, )];
NSInteger num = y+*x+;
button.tag = num;
[button addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
UIColor *colorNormal = [UIColor whiteColor];// [UIColor colorWithRed:252/255.0 green:252/255.0 blue:252/255.0 alpha:1];
UIColor *colorHightlighted = [UIColor colorWithRed:/ green:/ blue:/ alpha:];
if (num == || num == ) {
UIColor *colorTemp = colorNormal;
colorNormal = colorHightlighted;
colorHightlighted = colorTemp;
}
button.backgroundColor = colorNormal;
CGSize imageSize = CGSizeMake(frameW, );
UIGraphicsBeginImageContextWithOptions(imageSize, , [UIScreen mainScreen].scale);
[colorHightlighted set];
UIRectFill(CGRectMake(, , imageSize.width, imageSize.height));
UIImage *pressedColorImg = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[button setImage:pressedColorImg forState:UIControlStateHighlighted];
if (num<) {
UILabel *labelNum = [[UILabel alloc] initWithFrame:CGRectMake(, , frameW, )];
labelNum.text = [NSString stringWithFormat:@"%ld",num];
labelNum.textColor = [UIColor blackColor];
labelNum.textAlignment = NSTextAlignmentCenter;
labelNum.backgroundColor = [UIColor clearColor];
labelNum.font = kNumFont;
[button addSubview:labelNum];
if (num != ) {
UILabel *labelLetter = [[UILabel alloc] initWithFrame:CGRectMake(, , frameW, )];
labelLetter.text = [arrLetter objectAtIndex:num-];
labelLetter.textColor = [UIColor blackColor];
labelLetter.textAlignment = NSTextAlignmentCenter;
labelLetter.font = [UIFont systemFontOfSize:];
labelLetter.backgroundColor = [UIColor clearColor];
[button addSubview:labelLetter];
}
} else if (num == ) {
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(, , frameW, )];
label.text = @"0";
label.textColor = [UIColor blackColor];
label.textAlignment = NSTextAlignmentCenter;
label.backgroundColor = [UIColor clearColor];
label.font = kNumFont;
[button addSubview:label];
} else if (num == ) {
_customLbl = [[UILabel alloc] initWithFrame:CGRectMake(, , frameW, )];
_customLbl.text = @"完成";
_customLbl.backgroundColor = [UIColor clearColor];
_customLbl.textColor = [UIColor blackColor];
_customLbl.textAlignment = NSTextAlignmentCenter;
[button addSubview:_customLbl];
} else {
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(btnLong:)];
longPress.minimumPressDuration = ; //定義按的時間
[button addGestureRecognizer:longPress];
UIImageView *arrow = [[UIImageView alloc] initWithFrame:CGRectMake((SCREENWIDTH/ - ) / , , , )];
arrow.image = [UIImage imageNamed:@"arrowInKeyboard"];
[button addSubview:arrow];
}
return button;
}
- (void)btnLong:(UILongPressGestureRecognizer *)longPress {
[self.delegate numberKeyboardBackspaceTag:];
}
-(void)clickButton:(UIButton *)sender {
if (sender.tag == ) {
[self.delegate changeKeyboardType];
return;
} else if(sender.tag == ) {
[self.delegate numberKeyboardBackspaceTag:sender.tag];
} else {
NSInteger num = sender.tag;
if (sender.tag == )
num = ;
[self.delegate numberKeyboardInput:num];
}
}
@end
裡都很簡單是以沒加什麼注釋!
不明白的可以找我 QQ:2298527161