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 ()

@property(nonatomic, weak) UIView * textInputView;

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

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSArray *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

你可能感兴趣的:(iOS自定义键盘-简单版)