很多时候我们都需要一个自定义的键盘来输入金额,并且要每三位加逗号隔开,下面我写了一个demo,供大家参考,废话不说,附上主要代码.
self.btnTitles = @[@"7",@"8",@"9",@"4",@"5",@"6",@"1",@"2",@"3",@"0",@"10",@"11",@"C",@"清除"];
for (int i = 0; i < self.btnTitles.count - 2; i++) {
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
NSString *number = [self.btnTitles objectAtIndex:i];
[btn setTag:number.integerValue];
[btn setBackgroundColor:[UIColor whiteColor]];
[btn setContentMode:UIViewContentModeBottom];
[btn setFrame:CGRectMake((i%3) * BTN_WIDTH, (i/3) * BTN_HEIGHT + height, BTN_WIDTH, BTN_HEIGHT)];
[btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%@",number]];
UIView *view = [[UIView alloc] init];
[view setUserInteractionEnabled:NO];
view.layer.contents = (id)image.CGImage;
[view setBounds:CGRectMake(0, 0, image.size.width, image.size.height)];
view.center = CGPointMake(BTN_WIDTH/2.f, BTN_HEIGHT /2.f);
[btn addSubview:view];
}
for (int i = 0; i < self.btnTitles.count - 12; i++) {
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setTag:i + 12];
[btn setBackgroundColor:[UIColor whiteColor]];
[btn setContentMode:UIViewContentModeBottom];
[btn setFrame:CGRectMake(3 * BTN_WIDTH, i * 2 * BTN_HEIGHT + height, BTN_WIDTH, 2 * BTN_HEIGHT)];
[btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
UIImage *image = [UIImage imageNamed:[self.btnTitles objectAtIndex:i + 12]];
UIView *view = [[UIView alloc] init];
[view setUserInteractionEnabled:NO];
view.layer.contents = (id)image.CGImage;
[view setBounds:CGRectMake(0, 0, image.size.width, image.size.height)];
view.center = CGPointMake(BTN_WIDTH/2.f, BTN_HEIGHT);
[btn addSubview:view];
}
for (int i = 0; i < 2; i++) {
UIView *line = [[UIView alloc] initWithFrame:CGRectMake(BTN_WIDTH * i, height, LINE_HEIGTH, BTN_HEIGHT * 4)];
[line setBackgroundColor:[UIColor lightGrayColor]];
[self.view addSubview:line];
}
for (int i = 0; i < 5; i++) {
CGFloat w;
if ((i+1) % 2 == 0) {
w = SCREEN_WIDTH - BTN_WIDTH;
}else {
w = SCREEN_WIDTH;
}
UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, height + i*BTN_HEIGHT, w, LINE_HEIGTH)];
[line setBackgroundColor:[UIColor lightGrayColor]];
[self.view addSubview:line];
}
for (int i = 1; i < 4; i++) {
UIView *line = [[UIView alloc] initWithFrame:CGRectMake(BTN_WIDTH * i, height, LINE_HEIGTH, BTN_HEIGHT * 4)];
[line setBackgroundColor:[UIColor lightGrayColor]];
[self.view addSubview:line];
}
以上是布局键盘,下面是键盘的点击事件.
- (void)btnAction:(UIButton *)btn
{
switch (btn.tag) {
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:{
if(self.isFloat){
if(self.numerical % 100 == 0 && self.floatTen == -1 && self.floatDigits == -1){//刚点击完小数点
self.numerical = self.numerical + btn.tag*10;
self.floatTen = btn.tag;
}else if(self.numerical % 10 == 0 && self.floatDigits == -1){//小数点后一位
self.numerical = self.numerical + btn.tag*1;
self.floatDigits = btn.tag;
}else { //小数点后两位
//已经达到小数点后两位不能输入
}
}else {
if([self getIntPartLength] < MAX_COUNT_LENGTH)//小于最大值
self.numerical = self.numerical*10 + btn.tag*100;
}
}
break;
case 10:{//点
self.isFloat = YES;
}
break;
case 11://00
if(!self.isFloat){
if([self getIntPartLength] < MAX_COUNT_LENGTH - 1){
self.numerical *= 100;
}else if([self getIntPartLength] < MAX_COUNT_LENGTH){
self.numerical *= 10;
}
}
break;
case 12://C
self.numerical = 0;
self.isFloat = NO;
self.floatTen = -1;
self.floatDigits = -1;
break;
case 13://清除
if(!self.isFloat){
if(self.numerical >= 1000){
NSInteger floatPart = self.numerical % 100;
self.numerical /= 1000;
self.numerical =self.numerical*100 + floatPart ;
}else if(self.numerical >=100){
self.numerical %= 100;
}
}else {
if(self.numerical % 100 == 0 && self.floatTen == -1 && self.floatDigits == -1){//刚点击完小数点
self.isFloat = NO;
self.numerical /= 1000;
self.numerical *= 100;
self.floatTen = -1;
self.floatDigits = -1;
}else if(self.numerical % 10 == 0 && self.floatDigits == -1){//小数点后一位
self.numerical /= 100;
self.numerical *= 100;
self.floatTen = -1;
}else { //小数点后两位
self.numerical /= 10;
self.numerical *= 10;
self.floatDigits = -1;
}
}
break;
default:
break;
}
[self.labNumber setText:self.showStr];
}
我知道很多童鞋喜欢看demo来理解代码,下面附上github地址,仅供参考.
github地址:https://github.com/ZSyingyu/CustomKeyboardDemo.git
网友评论