iOS自定义数字键盘

自定义键盘实际就是设置UITextField的inputView属性,首先我们要做的是自定义一个UIView,实现键盘的样式。

自定义View代码如下:

#import <UIKit/UIKit.h>
//创建自定义键盘协议
@protocol My_KeyBoardDelegate <NSObject>
//创建协议方法
@required//必须执行的方法
- (void)numberKeyBoard:(NSInteger) number;
- (void)cancelKeyBoard;
- (void)finishKeyBoard;
- (void)periodKeyBoard;
- (void)changeKeyBoard;
@optional//不必须执行方法

@end

@interface My_KeyBoardView : UIView
{
@private//私有的协议方法
    id<My_KeyBoardDelegate> _delegate;
}
@property (nonatomic, strong) id<My_KeyBoardDelegate> delegate;

- (id)initWithNumber:(NSNumber *)number;
@end
#import "My_KeyBoardView.h"
#define kScreenWidth   [UIScreen mainScreen].bounds.size.width
#define kScreenHeight  [UIScreen mainScreen].bounds.size.height

@implementation My_KeyBoardView


- (id)initWithNumber:(NSNumber *)number;
{
    self = [super init];
    if (self) {
        
        self.backgroundColor = [UIColor redColor];
        self.frame = CGRectMake(0, kScreenHeight - 150, kScreenHeight, 150);
        
            [self initKeyBoardNumber_1];
    }
    return self;
}
- (void)initKeyBoardNumber_1
{
    self.frame=CGRectMake(0, kScreenHeight-243, kScreenWidth, 243);
    int space=1;
    for (int i=0; i<9; i++) {
        NSString *str=[NSString stringWithFormat:@"%d",i+1];
        UIButton *button=[UIButton buttonWithType:UIButtonTypeSystem];
        if (i<3) {
            button.frame=CGRectMake(i%3*(kScreenWidth/4)+space, i/3*61, kScreenWidth/4-1, 60);
        }
        else{
            button.frame=CGRectMake(i%3*(kScreenWidth/4)+space, i/3*60+i/3*space, kScreenWidth/4-1, 60);
        }
        button.backgroundColor=[UIColor whiteColor];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        button.titleLabel.font=[UIFont systemFontOfSize:24];
        [button setTitle:str forState:UIControlStateNormal];
        button.tag=i+1;
        [button addTarget:self action:@selector(keyBoardAciont:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:button];
    }
    UIButton *dian=[UIButton buttonWithType:UIButtonTypeSystem];
    dian.frame=CGRectMake(space,60*3+3 , kScreenWidth/4-1, 60);
    dian.backgroundColor=[UIColor whiteColor];
    [dian setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    dian.titleLabel.font=[UIFont systemFontOfSize:24];
    [dian addTarget:self action:@selector(keyBoardAciont:) forControlEvents:UIControlEventTouchUpInside];
    [dian setTitle:@"." forState:UIControlStateNormal];
    dian.tag=11;
    [self addSubview:dian];
    UIButton *ling=[UIButton buttonWithType:UIButtonTypeSystem];
    ling.frame=CGRectMake(kScreenWidth/4+1*space,60*3+3, kScreenWidth/4-1, 60);
    ling.backgroundColor=[UIColor whiteColor];
    [ling setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    ling.titleLabel.font=[UIFont systemFontOfSize:24];
    [ling setTitle:@"0" forState:UIControlStateNormal];
    ling.tag=0;
    [ling addTarget:self action:@selector(keyBoardAciont:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:ling];
    
    UIButton *computer=[UIButton buttonWithType:UIButtonTypeSystem];
    computer.frame=CGRectMake(kScreenWidth/4*2+space,60*3+3, kScreenWidth/4-1, 60);
    computer.backgroundColor=[UIColor whiteColor];
    [computer setTitle:@"系统键盘" forState:UIControlStateNormal];
    computer.tag=12;
    [computer addTarget:self action:@selector(keyBoardAciont:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:computer];
    
    UIButton *delete=[UIButton buttonWithType:UIButtonTypeSystem];
    delete.frame=CGRectMake(kScreenWidth/4*3+space,1, kScreenWidth/4-1, 122);
    [delete addTarget:self action:@selector(keyBoardAciont:) forControlEvents:UIControlEventTouchUpInside];
    delete.tag=10;
    
    UIImageView *deleteImage=[[UIImageView alloc]initWithFrame:CGRectMake((kScreenWidth/4-1 - 28) * 1.0 / 2, 50, 28, 20)];
    deleteImage.image=[UIImage imageNamed:@"goumai_03"];
    [delete addSubview:deleteImage];
    
    // [delete setBackgroundImage:[UIImage imageNamed:@"goumai_03"] forState:UIControlStateNormal];
    [self addSubview:delete];
    
    UIButton *confirm=[UIButton buttonWithType:UIButtonTypeSystem];
    confirm.frame=CGRectMake(kScreenWidth/4*3+space,61*2, kScreenWidth/4-1, 122);
    confirm.backgroundColor=[UIColor blueColor];
    [confirm setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    confirm.titleLabel.font=[UIFont systemFontOfSize:20];
    [confirm setTitle:@"确 定" forState:UIControlStateNormal];
    [confirm addTarget:self action:@selector(keyBoardAciont:) forControlEvents:UIControlEventTouchUpInside];
    confirm.tag=13;
    [self addSubview:confirm];
}

#pragma 键盘点击按钮事件
- (void)keyBoardAciont:(UIButton *)sender
{
    UIButton* btn = (UIButton*)sender;
    NSInteger number = btn.tag;
    // no delegate, print log info
    if (nil == _delegate) {
        NSLog(@"button tag [%ld]",(long)number);
        return;
    }
    
    if (number <= 9 && number >= 0) {
        [_delegate numberKeyBoard:number];
        return;
    }
    
    if (10 == number) {
        [_delegate cancelKeyBoard];
        return;
    }
    if (11 == number) {
        [_delegate periodKeyBoard];
        return;
    }
    if (12 == number) {
        [_delegate changeKeyBoard];
        return;
    }
    
    if (13 == number) {
        [_delegate finishKeyBoard];
        return;
    }
    
}
@end
#define kScreenWidth   [UIScreen mainScreen].bounds.size.width
#define kScreenHeight  [UIScreen mainScreen].bounds.size.height

#import "ViewController.h"
#import "My_KeyBoardView.h"
@interface ViewController ()<My_KeyBoardDelegate,UITextFieldDelegate>
@property (nonatomic, strong) My_KeyBoardView *my_keyboard;
@property (nonatomic, strong) UITextField *textField;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.textField = [[UITextField alloc] initWithFrame:CGRectMake(50, 168, kScreenWidth - 100, 50)];
    self.textField.delegate = self;
    self.textField.backgroundColor = [UIColor greenColor];
    self.textField.placeholder = @"(默认系统键盘)";
    
    [self.view addSubview:self.textField];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hidenKeyBoard)];
    [self.view addGestureRecognizer:tap];
    
    [self keyBoardTypeAction];
}
- (void)keyBoardTypeAction
{

        self.my_keyboard = [[My_KeyBoardView alloc] initWithNumber:@1];
        self.textField.inputView = self.my_keyboard;
        self.my_keyboard.delegate = self;
        [self.textField reloadInputViews];
}
- (void)hidenKeyBoard
{
    NSLog(@"键盘隐藏");
    [self.textField resignFirstResponder];
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    NSLog(@"显示键盘");
}

- (void)numberKeyBoard:(NSInteger)number
{
    NSString *str = self.textField.text;
    self.textField.text = [NSString stringWithFormat:@"%@%ld",str,(long)number];
}

- (void)cancelKeyBoard
{
    
    NSMutableString *muStr = [[NSMutableString alloc] initWithString:self.textField.text];
    if (muStr.length <= 0) {
        return;
    }
    [muStr deleteCharactersInRange:NSMakeRange([muStr length] - 1, 1)];
    self.textField.text = muStr;
}

#pragma 输入点
-(void)periodKeyBoard{
    
    if ([self.textField.text isEqualToString:@""]) {
        return;
    }
    
    //判断当前时候存在一个点
    if ([self.textField.text rangeOfString:@"."].location == NSNotFound) {
        //输入中没有点
        NSMutableString  *mutableString=[[NSMutableString alloc]initWithFormat:@"%@%@",self.textField.text,@"."];
        self.textField.text=mutableString;
    }
}
-(void)changeKeyBoard{
    self.textField.inputView = nil;
    [self.textField reloadInputViews];
}

-(void)finishKeyBoard{
    [self.view endEditing:YES];
}

@end

实现的效果:

特别声明:文章多为网络转载,资源使用一般不提供任何帮助,特殊资源除外,如有侵权请联系!
技术狗 » iOS自定义数字键盘