iOS自定义键盘-简单版

为什么说是简单版,因为这里只说一个数字键盘。
一,怎么自定义键盘
随便一个view都可以作为键盘,主要代码是为你的输入框指定inputView,这个inputView就是键盘,键盘具体什么样子都可以。

kfZNumberKeyBoard * mkb = [kfZNumberKeyBoard moneyKeyBoardBuyer];
    
UITextField * field = [[UITextField alloc]init];
field.backgroundColor = [UIColor cyanColor];
field.inputView = mkb;
[self.view addSubview:field];
field.frame = CGRectMake(20, NavBottom + 50, DEF_SCREEN_WIDTH - 40, 40);

二,自定义键盘怎么实现各种输入
这里千万不要自己拼接字符串太容易出问题了,用系统自带的方法。我们发现不管UITextField还是UITextView都遵循UITextInput协议,这个协议又遵循UIKeyInput协议,我们用的就是UIKeyInput协议中的方法。

- (void)insertText:(NSString *)text;//插入文字,不用处理光标位置
- (void)deleteBackward;//删除,不用处理光标位置

用这两个方法是不是事情就特别简单了,其实说到这里已经可以了,怎么做都说完了。不过我还是推销一下我写的数字键盘吧。最后面我会贴出代码用的可以拷贝改一下。
三,数字键盘
先看效果图:


image.png

a.UI布局上,删除和确定是单独的按键,其他部分我用了collectionView,想着之后做的乱序加密效果好做,打乱数据源刷新一下就行(当然现在没有,不是懒,过渡开发是病)
b.获取当前输入框,这里为了不在外面传,直接在内部监听了输入框开始输入和结束输入。
c.加了几个输入限制:
1.有小数点不能在输入小数点
2.内容为空输入小数点时,前面自动补0
3.最大小数位数限制(测试不多可能有bug哦)
4.移除焦点时小数点前面没东西自动补0
5.输入框有内容确定可以点击,输入框没内容确定不能点击。

下面是代码了:

@interface kfZNumberKeyBoard : UIView

/** 确认按键 */
@property (nonatomic, strong) UIButton * returnButton;
/** 有没有小数点 */
@property (nonatomic, assign) BOOL hiddenPoint;
/** 小数位数,为0不限制,不需要小数时请使用hiddenPoint隐藏点 默认是2 */
@property (nonatomic, assign) NSUInteger decimalCount;
/** 整体高度 */
@property (nonatomic, assign, readonly) CGFloat KFZNumberKeyBoardHeight;

+(instancetype)moneyKeyBoardBuyer;
+(instancetype)moneyKeyBoardSeller;

-(instancetype)initWitHiddenPoint:(BOOL)hiddenPoint;

@end
#import "kfZNumberKeyBoard.h"
#import "KFZKeyBoardCell.h"
@interface kfZNumberKeyBoard ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property(nonatomic, weak) UIView<UITextInput> * textInputView;

/** 删除按键 */
@property (nonatomic, strong) UIButton * deleteButton;

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSArray<NSNumber*> *dataSource;

/** 间隔 */
@property (nonatomic, assign) CGFloat KFZNumberKeyBoardSpace;
/** 数字按键高度 */
@property (nonatomic, assign) CGFloat KFZNumberKeyBoardItemHeight;

@end

@implementation kfZNumberKeyBoard

+(instancetype)moneyKeyBoardBuyer{
    kfZNumberKeyBoard * keyBoard = [[kfZNumberKeyBoard alloc]initWitHiddenPoint:NO];
    return keyBoard;
}

+(instancetype)moneyKeyBoardSeller{
    kfZNumberKeyBoard * keyBoard = [[kfZNumberKeyBoard alloc]initWitHiddenPoint:NO];
    keyBoard.returnButton.backgroundColor = [UIColor maintonal_sellerMain];
    return keyBoard;
}
       
-(instancetype)initWitHiddenPoint:(BOOL)hiddenPoint{
    self = [super init];
    if (self) {
        _hiddenPoint = hiddenPoint;
        _KFZNumberKeyBoardItemHeight = 50.f;
        _KFZNumberKeyBoardSpace = 0.5;
        _KFZNumberKeyBoardHeight = _KFZNumberKeyBoardItemHeight * 4 + _KFZNumberKeyBoardSpace * 5 + HOMEINDICATOR_HEIGHT;
        _decimalCount = 2;
        
        self.frame = CGRectMake(0, 0, DEF_SCREEN_WIDTH, _KFZNumberKeyBoardHeight);
        
        _deleteButton = [[UIButton alloc]init];
        _deleteButton.backgroundColor = [UIColor color_FAFAFA];
        [_deleteButton setImage:[UIImage imageNamed:@"keyboard_icon_backspace"] forState:UIControlStateNormal];
        [_deleteButton addTarget:self action:@selector(deleteEvent) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_deleteButton];
        [_deleteButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(_KFZNumberKeyBoardSpace);
            make.right.mas_equalTo(0.f);
            make.width.equalTo(self).multipliedBy(0.25);
        }];
        
        _returnButton = [[UIButton alloc]init];
        [_returnButton setTitle:@"确定" forState:UIControlStateNormal];
        [_returnButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _returnButton.titleLabel.font = [UIFont custemFontOfSize:20 weight:UIFontWeightRegular];
        _returnButton.backgroundColor = [UIColor mainTonal_main];
        [_returnButton addTarget:self action:@selector(returnEvent) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_returnButton];
        [_returnButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_deleteButton.mas_bottom);
            make.right.equalTo(_deleteButton);
            make.bottom.mas_equalTo(-HOMEINDICATOR_HEIGHT);
            make.height.equalTo(_deleteButton);
            make.width.equalTo(_deleteButton).offset(_KFZNumberKeyBoardSpace);
        }];
        
        //101对应小数点 102对应收起键盘 修改的话其他的判断逻辑也要修改
        _dataSource = @[@(1), @(2), @(3), @(4), @(5), @(6), @(7), @(8), @(9), @(101), @(0), @(102)];
        
        UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init];
        layout.itemSize = CGSizeMake((DEF_SCREEN_WIDTH * 3.f/4.f - _KFZNumberKeyBoardSpace*3)/3.f, (_KFZNumberKeyBoardHeight - HOMEINDICATOR_HEIGHT - _KFZNumberKeyBoardSpace*5)/4.f);
        layout.sectionInset = UIEdgeInsetsMake(_KFZNumberKeyBoardSpace, 0, _KFZNumberKeyBoardSpace, _KFZNumberKeyBoardSpace);
        layout.minimumLineSpacing = _KFZNumberKeyBoardSpace;
        layout.minimumInteritemSpacing = _KFZNumberKeyBoardSpace;
        
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        [_collectionView registerClass:[KFZKeyBoardCell class] forCellWithReuseIdentifier:NSStringFromClass([KFZKeyBoardCell class])];
        _collectionView.backgroundColor = [UIColor clearColor];
        _collectionView.scrollEnabled = NO;
        [self addSubview:_collectionView];
        [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.mas_equalTo(0.f);
            make.bottom.mas_equalTo(-HOMEINDICATOR_HEIGHT);
            make.right.equalTo(_deleteButton.mas_left);
        }];
        
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textInputViewDidBeginEditing:) name:UITextFieldTextDidBeginEditingNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textInputViewDidEndEditing:) name:UITextFieldTextDidEndEditingNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textInputViewDidBeginEditing:) name:UITextViewTextDidBeginEditingNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textInputViewDidEndEditing:) name:UITextViewTextDidEndEditingNotification object:nil];
    }
    return self;
}

-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextFieldTextDidBeginEditingNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextFieldTextDidEndEditingNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextViewTextDidBeginEditingNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextViewTextDidEndEditingNotification object:nil];
}

#pragma mark - response

-(void)textInputWithNumber:(NSNumber *)number{
    NSString *strValue = [self inputViewString];
    
    if ([number isEqualToNumber:@(101)]) {
        if ([strValue containsString:@"."]){
            return;
        }else{
            if ([strValue length] <= 0)
                [self.textInputView insertText:@"0."];
            else
                [self.textInputView insertText:@"."];
        }
    }else{
        if ([strValue containsString:@"."] && _decimalCount > 0) {
            NSInteger pointLocation = [strValue rangeOfString:@"."].location;
            NSInteger curDecimalCount = strValue.length - pointLocation - 1;
            if (curDecimalCount >= _decimalCount) {
                 NSInteger cursorLocation = [self inputViewSelectRangeLocation];
                if (cursorLocation <= pointLocation) {
                    [_textInputView insertText:number.stringValue];
                }
            }else{
                [_textInputView insertText:number.stringValue];
            }
        }else{
          [_textInputView insertText:number.stringValue];
        }
    }
    [self freshReturnButtonEnabled];
}

-(void)deleteEvent{
    [_textInputView deleteBackward];
    [self freshReturnButtonEnabled];
}

-(void)returnEvent{
    [_textInputView resignFirstResponder];
}

-(void)textInputViewDidBeginEditing:(NSNotification*)notification{
    _textInputView = notification.object;
    [self freshReturnButtonEnabled];
}

-(void)textInputViewDidEndEditing:(NSNotification*)notification{
    NSString *strValue = [self inputViewString];
    if ([strValue startsWithString:@"."]) {
        strValue = [NSString stringWithFormat:@"0%@", strValue];
        [self setInputViewString:strValue];
    }
    _textInputView = nil;

}

-(NSString *)inputViewString{
    NSString *strValue = @"";
    if ([self.textInputView isKindOfClass:[UITextView class]]){
        strValue = ((UITextView *)self.textInputView).text;
    }else if ([self.textInputView isKindOfClass:[UITextField class]]){
        strValue = ((UITextField *)self.textInputView).text;
    }
    return strValue;
}

-(void)setInputViewString:(NSString *)string{
    if ([self.textInputView isKindOfClass:[UITextView class]]){
        ((UITextView *)self.textInputView).text = string;
    }else if ([self.textInputView isKindOfClass:[UITextField class]]){
        ((UITextField *)self.textInputView).text = string;
    }
}

-(NSInteger)inputViewSelectRangeLocation{
    NSInteger location = 0;
    if ([self.textInputView isKindOfClass:[UITextView class]]){
        UITextView * textView = (UITextView *)self.textInputView;
        location = textView.selectedRange.location;
    }else if ([self.textInputView isKindOfClass:[UITextField class]]){
        UITextField *textField = (UITextField *)self.textInputView;
        UITextPosition* beginning = textField.beginningOfDocument;
        UITextRange* selectedRange = textField.selectedTextRange;
        UITextPosition* selectionStart = selectedRange.start;
        location = [textField offsetFromPosition:beginning toPosition:selectionStart];
    }
    return location;
}

-(void)freshReturnButtonEnabled{
    NSString *strValue = [self inputViewString];
    if (strValue.length == 0) {
        _returnButton.enabled = NO;
        _returnButton.alpha = 0.6;
    }else{
        _returnButton.enabled = YES;
        _returnButton.alpha = 1.f;
    }
}

#pragma mark -- Delegate
#pragma mark - UICollectionViewDataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataSource.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    KFZKeyBoardCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([KFZKeyBoardCell class]) forIndexPath:indexPath];
    NSNumber * number = self.dataSource[indexPath.row];
    if ([number isEqualToNumber:@(101)] && _hiddenPoint) {
        cell.textLabel.text = @"";
    }else{
        cell.textNumber = number;
    }
    return cell;
}

#pragma mark - UICollectionViewDelegate

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSNumber * number = self.dataSource[indexPath.row];
    if ([number isEqualToNumber:@(102)]) {
        [_textInputView resignFirstResponder];
        return;
    }
    if ([number isEqualToNumber:@(101)] && _hiddenPoint) {
        return;
    }
    [self textInputWithNumber:number];
}

#pragma mark - init

-(void)setHiddenPoint:(BOOL)hiddenPoint{
    _hiddenPoint = hiddenPoint;
    [_collectionView reloadData];
}

@end

这个是里面cell的:

@interface KFZKeyBoardCell : UICollectionViewCell
/** 文字 */
@property (nonatomic, strong) UILabel * textLabel;
/** 图片 */
@property (nonatomic, strong) UIImageView * imageIcon;

/** 设置值 */
@property (nonatomic, strong) NSNumber * textNumber;
@end
#import "KFZKeyBoardCell.h"

@implementation KFZKeyBoardCell

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor color_FAFAFA];
        
        [self.contentView addSubview:self.textLabel];
        [self.textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.left.right.mas_equalTo(0.f);
        }];
        
        self.imageIcon.hidden = YES;
        [self.contentView addSubview:self.imageIcon];
        [self.imageIcon mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.mas_equalTo(CGPointZero);
            make.size.mas_equalTo(CGSizeMake(24.f, 22.f));
        }];
    }
    return self;
}
-(void)prepareForReuse{
    self.textLabel.hidden = NO;
    self.imageIcon.hidden = YES;
}
- (void)setTextNumber:(NSNumber *)textNumber{
    _textNumber = textNumber;
    if ([textNumber isEqualToNumber:@(101)]) {
        self.textLabel.text = @"·";
    }
    else if ([textNumber isEqualToNumber:@(102)]){
        self.textLabel.hidden = YES;
        self.imageIcon.hidden = NO;
        self.imageIcon.image = [UIImage imageNamed:@"keyboard_icon_smallkb"];
    }
    else{
        self.textLabel.text = textNumber.stringValue;
    }
}

- (UILabel *)textLabel{
    if (!_textLabel) {
        _textLabel = [[UILabel alloc]init];
        _textLabel.font = [UIFont KFZSpecial_DINAlternateBoldWithFontSize:24.f];
        _textLabel.textAlignment = NSTextAlignmentCenter;
        _textLabel.userInteractionEnabled = NO;
        _textLabel.backgroundColor = UIColor.clearColor;
    }
    return _textLabel;
}

-(UIImageView *)imageIcon{
    if (!_imageIcon) {
        _imageIcon = [[UIImageView alloc]init];
    }
    return _imageIcon;
}

@end
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 157,298评论 4 360
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 66,701评论 1 290
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 107,078评论 0 237
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,687评论 0 202
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,018评论 3 286
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,410评论 1 211
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,729评论 2 310
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,412评论 0 194
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,124评论 1 239
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,379评论 2 242
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,903评论 1 257
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,268评论 2 251
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,894评论 3 233
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,014评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,770评论 0 192
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,435评论 2 269
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,312评论 2 260