ios自定义数字键盘

.h文件

#import <UIKit/UIKit.h>
@class KeyBoardView;

@protocol KeyBoardViewDelegate <NSObject>

- (BOOL)keyBoardView:(KeyBoardView *)keyBoardView shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;

@end

@interface KeyBoardView : UIView

@property (nonatomic, assign) id <KeyBoardViewDelegate> delegate;

/**
 字符串
 */
@property (nonatomic, strong) NSMutableString *string;

@end

.m文件

#import "KeyBoardView.h"
#import <Foundation/Foundation.h>

@implementation KeyBoardView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.string = [NSMutableString string];
        self.frame = frame;
        self.backgroundColor = [UIColor colorWithRed:206.0 / 255 green:206.0 / 255 blue:206.0 / 255 alpha:1];
        [self initKeyBoardView];
    }
    return self;
}

/**
 初始化键盘
 */
- (void)initKeyBoardView {
    //set the key titles
    NSArray *titleArray = [[NSArray alloc] initWithObjects:@"1", @"2", @"3", @"4", @"5", @"6", @"7", @"8", @"9", @" ", @"0", @"", nil];
    
    //design the keyboard
    int index = 0;
    float button_width = (self.bounds.size.width - 3) / 3;
    float button_height = (self.bounds.size.height - 4) / 4;
    //12
    for (int i = 0; i < 4; i++) {
        for (int j = 0; j < 3; j++) {
            float x = 0 + j * (button_width + 1);
            float y = 0 + i * (button_height + 1);
            UIButton *button = [self addButtonWithTitle:titleArray[index]
                                                  frame:CGRectMake(x, y, button_width, button_height)
                                                  image:nil
                                              highImage:[UIImage imageNamed:@"bgcolor.png"]];
            //设置删除按钮
            if (i == 3 && j == 2) {
                UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"keyboardDel.png"]];
                image.center = CGPointMake(button.bounds.size.width / 2, button.bounds.size.height / 2);
                [button addSubview:image];
            }
            //设置颜色
            if ((i == 3 && j == 0) || (i == 3 && j == 2)) {
                button.backgroundColor = [UIColor colorWithRed:209.0 / 255 green:212.0 / 255 blue:218.0 / 255 alpha:1];
            } else {
                button.backgroundColor = [UIColor whiteColor];
            }
            [button addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
            [self addSubview:button];
            index++;
        }
    }
}

/**
 设置按钮

 @param title 设置title 12334...
 @param frame_rect 按钮rect
 @param normal_image 按钮的正常图片
 @param high_image 按钮按下图片
 @return 按钮
 */
- (UIButton *)addButtonWithTitle:(NSString *)title
                           frame:(CGRect)frame_rect
                           image:(UIImage *)normal_image
                       highImage:(UIImage *)high_image {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setFrame:frame_rect];
    button.titleLabel.font = [UIFont boldSystemFontOfSize:20];
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button setBackgroundImage:normal_image forState:UIControlStateNormal];
    [button setBackgroundImage:high_image forState:UIControlStateHighlighted];
    
    return button;
}

/**
 点击按钮事件

 @param button 按钮
 */
- (void)onClick:(UIButton *)button {
    //左下角空白键不允许按 并且当当前没输入任何内容不允许输入x
    if (![button.currentTitle isEqualToString:@" "] &&
        !(self.string.length == 0 && [button.currentTitle isEqualToString:@""])) {
        
        [[self string] appendString:button.currentTitle];
        NSString *str = button.currentTitle;
        //call the delegate, first make sure it can respond to selector, then do the delegate method
        if ([self.delegate respondsToSelector:@selector(keyBoardView:shouldChangeCharactersInRange:replacementString:)]) {
            BOOL canLogin = [self.delegate keyBoardView:self shouldChangeCharactersInRange:NSMakeRange(self.string.length - 1, 1) replacementString:str];
            if (canLogin && button.currentTitle.length == 0) {
                //按x 减1
                [self.string deleteCharactersInRange:NSMakeRange(self.string.length - 1, 1)];
            }
        }
    }
}

调用

    _keyBoard = [[KeyBoardView alloc] initWithFrame:CGRectMake(0, ScreenHeight - KEYBOARD_HEIGHT, ScreenWidth, KEYBOARD_HEIGHT)];
    [self.view addSubview:_keyBoard];
    _keyBoard.delegate = self;

代理方法

/**
 *  输入密码textfiled,6个数字以上直接发送请求
 *
 *  @param textField 输入密码textfiled
 *  @param range     输入范围
 *  @param string    输入数字
 *
 *  @return 是否允许输入
 */
- (BOOL)keyBoardView:(KeyBoardView *)keyBoardView shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
  //return NO;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 来自产品经理的"简单"需求一则 需求:在输入身份证号码的时候,弹出来的键盘是能够切换到字母的九宫格数字键盘。(左边...
    伊尔今夏阅读 10,590评论 5 33
  • 在开发中经常碰到一些会用到自定义的数字键盘的,这些一般都是随机或者按照一定的规则来生成的数字键盘,这段时间刚好有这...
    fuaiyi阅读 2,233评论 0 2
  • 因为项目需求,简单的弄了个自定义数字键盘 需要的自取,大神勿喷 使用也很简单 这里是GitHub地址
    _Comma阅读 892评论 1 2
  • *面试心声:其实这些题本人都没怎么背,但是在上海 两周半 面了大约10家 收到差不多3个offer,总结起来就是把...
    Dove_iOS阅读 27,241评论 30 472
  • 到网站 http://idea.lanyus.com/ 获取注册码。2.填入下面的license server:h...
    TY_阅读 537评论 0 0