IOS-OC 自定义数字键盘

  1. //宏定义屏幕的宽和高
    //屏幕宽度

define kwidth self.view.frame.size.width

//键盘的高度

define Kheight 216

2 我们先定义两个textField:用来接收键盘的输入值
UITextField * textfield2 = [[UITextField alloc] initWithFrame:CGRectMake(170, 240, 180, 40)];
textfield2.tag = 222;
textfield2.backgroundColor = [UIColor orangeColor];
textfield2.layer.cornerRadius = 5;
textfield2.layer.masksToBounds = YES;
textfield2.clearButtonMode = UITextFieldViewModeAlways;
[self.view addSubview:textfield2];

UITextField * textfield3 = [[UITextField alloc] initWithFrame:CGRectMake(170, 300, 100, 40)];
textfield3.tag = 223;
textfield3.placeholder = @"验证码";
textfield3.layer.cornerRadius = 5;
textfield3.layer.masksToBounds = YES;
textfield3.clearButtonMode = UITextFieldViewModeAlways;
textfield3.backgroundColor = [UIColor whiteColor];
[self.view addSubview:textfield3];

3 //键盘的辅助视图
//设置辅助视图的大小
UIView * grayView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 40)];
//辅助视图的背景颜色
grayView.backgroundColor = [UIColor lightGrayColor];
//.注意!!关键给键盘设置辅助视图
textfield2.inputAccessoryView = grayView;
textfield3.inputAccessoryView = grayView;

  1. //给键盘设置输入视图
    UIView * whiteView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 216)];
    whiteView.backgroundColor = [UIColor whiteColor];
    //.注意!!关键给键盘设置输入视图
    textfield2.inputView = whiteView;
    textfield3.inputView = whiteView;

//注意!!!!!!在这里我们用数组来接收1-9的值
NSArray * array = @[@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"×",@"0",@"return"];
//X号操作删除最后一个字符
//return 收回键盘
NSInteger index=0;
//设置每一行走的次数
for (int i = 0; i<4; i++) {
//设置每一趟走的次数
for (int j = 0; j<3; j++) {

        //设置按钮的位置---关键
        UIButton * button = [[UIButton alloc] initWithFrame: CGRectMake(kwidth / 3 * j, Kheight/4 * i,  kwidth/3, Kheight / 4)];

//数组按钮的颜色
button.backgroundColor = [UIColor whiteColor];
//数组按钮上的文字
[button setTitle:array[index] forState:UIControlStateNormal];
index++;//将数组的元素进行累加统计
//设置字体颜色
[button setTitleColor:[UIColor purpleColor] forState:UIControlStateNormal];
//加粗字体
button.titleLabel.font = [UIFont boldSystemFontOfSize:20];
//设置边框的颜色
button.layer.borderColor = [UIColor grayColor].CGColor;
//设置边框的宽度
button.layer.borderWidth = 3;

        //添加事件

//设置1-9的数字方法
if (i<3) {
[button addTarget:self action:@selector(b1Action:) forControlEvents:UIControlEventTouchUpInside];
}
else if(i == 3 && j == 0){
//叉号 用来删除字符
[button addTarget:self action:@selector(deleteChar:) forControlEvents:UIControlEventTouchUpInside];

        }
        else if( i == 3 && j ==1)
            {  //设置   0 的位置
                [button addTarget:self action:@selector(b1Action:) forControlEvents:UIControlEventTouchUpInside];
        }
        else{
        //return  用来收回键盘
            [button addTarget:self action:@selector(takeBackKeyboard:) forControlEvents:UIControlEventTouchUpInside];
       
        }
       
       
        [whiteView addSubview:button];
    }
}

}

pragma Mark ---拼接字符串

-(void)b1Action:(UIButton *)b1{
//点击当前按钮 就把按钮上的文字 拼接到输入框中
//1.获取输入框

UITextField *textField2 =  (UITextField *) [self.view viewWithTag:222];

if (textField2.isFirstResponder == YES) {
    textField2.text = [textField2.text stringByAppendingFormat:@"%@",b1.titleLabel.text];

}else {
    UITextField *textField3 =  (UITextField *) [self.view viewWithTag:223];
    textField3.text = [textField3.text stringByAppendingFormat:@"%@",b1.titleLabel.text];

}

}

pragma Mark ---删除最后一个字符

-(void)deleteChar:(UIButton *)button{

//2
 UITextField *textField2 =  (UITextField *) [self.view viewWithTag:222];
if (textField2.isFirstResponder == YES) {

if (textField2.text.length >0) {
    textField2.text = [textField2.text substringToIndex:textField2.text.length -1];
}
}

//3
else{
    UITextField *textField3 =  (UITextField *) [self.view viewWithTag:223];

    if (textField3.text.length >0) {
        textField3.text = [textField3.text substringToIndex:textField3.text.length -1];
    }
}

}

pragma Mark ---收回键盘

-(void)takeBackKeyboard:(UIButton *)button{
UITextField *textField2 = (UITextField *) [self.view viewWithTag:222];
//释放第一响应者 也就是收回键盘
if (textField2.isFirstResponder == YES) {
[textField2 resignFirstResponder];}

    else{
        UITextField *textField3 =  (UITextField *) [self.view viewWithTag:223];
  [textField3 resignFirstResponder];
}

}

效果如图:

IOS-OC 自定义数字键盘_第1张图片
屏幕快照 2017-08-04 下午3.53.30.png

你可能感兴趣的:(IOS-OC 自定义数字键盘)