ios如何 自定义键盘

样式

1.数字加字母

ios如何 自定义键盘

2.数字

ios如何 自定义键盘

3.数字加字母加部分符号

ios如何 自定义键盘

调用

ZQKeyBoardView *keyBoardView = [[ZQKeyBoardView alloc]initWithFrame:CGRectZero KeyBoardType:ZQKeyBoardTypeNumberAndLetter];
textField.inputView = keyBoardView;

设计代码

结构

ios如何 自定义键盘

思路

利用collectionView布局,生成每一行的button。

整体的代码写的比较粗糙,有时间会重构下,尽量写的简单优雅一些。

代码

ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController


@end

ViewController.m

//
//  ViewController.m
//  ZQKeyBoard
//
//  Created by 赵前 on 2019/4/17.
//  Copyright © 2019年 赵前. All rights reserved.
//

#import "ViewController.h"
#import "ZQKeyBoard/ZQKeyBoardView.h"

@interface ViewController ()

@property (nonatomic ,strong)UITextField *textField1;
@property (nonatomic ,strong)UITextField *textField2;
@property (nonatomic ,strong)UITextField *textField3;
@property (nonatomic ,strong)UITextField *textField4;


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.textField1];
    [self.view addSubview:self.textField2];
    [self.view addSubview:self.textField3];
    
   
}


- (UITextField *)textField1{
    if (!_textField1) {
        _textField1 = [[UITextField alloc]initWithFrame:CGRectMake(5, 100, self.view.bounds.size.width - 10, 40)];
        _textField1.backgroundColor = [UIColor greenColor];
        ZQKeyBoardView *keyBoardView = [[ZQKeyBoardView alloc]initWithFrame:CGRectZero KeyBoardType:ZQKeyBoardTypeNumberAndLetter];
        _textField1.inputView = keyBoardView;
    }
    return _textField1;
}

- (UITextField *)textField2{
    if (!_textField2) {
        _textField2 = [[UITextField alloc]initWithFrame:CGRectMake(5, 150, self.view.bounds.size.width - 10, 40)];
        _textField2.backgroundColor = [UIColor greenColor];
        ZQKeyBoardView *keyBoardView = [[ZQKeyBoardView alloc]initWithFrame:CGRectZero KeyBoardType:ZQKeyBoardTypeNumber];
        _textField2.inputView = keyBoardView;
    }
    return _textField2;
}

- (UITextField *)textField3{
    if (!_textField3) {
        _textField3 = [[UITextField alloc]initWithFrame:CGRectMake(5, 200, self.view.bounds.size.width - 10, 40)];
        _textField3.backgroundColor = [UIColor greenColor];
        ZQKeyBoardView *keyBoardView = [[ZQKeyBoardView alloc]initWithFrame:CGRectZero KeyBoardType:ZQKeyBoardTypeNumberAndLetterAndSymbol];
        _textField3.inputView = keyBoardView;
    }
    return _textField3;
}


@end

ZQKeyBoardView.h

//
//  ZQKeyBoardView.h
//  GSCB
//
//  Created by 赵前 on 2019/4/16.
//  Copyright © 2019年 赵前. All rights reserved.
//

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN



typedef NS_ENUM(NSUInteger, ZQKeyBoardType) {
    ZQKeyBoardTypeNumberAndLetter = 0,
    ZQKeyBoardTypeNumber,
    ZQKeyBoardTypeNumberAndLetterAndSymbol
};

@interface ZQKeyBoardView : UIView

@property (nonatomic, strong)UIColor* keyBoardBackGroundColor;


// 初始化
- (instancetype)initWithFrame:(CGRect)frame KeyBoardType:(ZQKeyBoardType)type;



@end

NS_ASSUME_NONNULL_END

ZQKeyBoardView.m

//
//  ZQKeyBoardView.m
//  GSCB
//
//  Created by 赵前 on 2019/4/16.
//  Copyright © 2019年 赵前. All rights reserved.
//

#import "ZQKeyBoardView.h"
#import "CustomFlowLayout.h"
#import "ZQKeyBoardCollectionViewCell.h"

#define SCREEN_WIDTH         [UIScreen mainScreen].bounds.size.width

#define BOARDRATIO           (224.0 / 275)           //键盘的高宽比
#define KEYRATIO             (86.0  / 63)            //按键的高宽比

#define KEYBOARD_HEIGHT      (SCREEN_WIDTH * BOARDRATIO)    //键盘的高

#define BTN_WIDTH            (SCREEN_WIDTH / 10.0 - 6) //按键的宽
#define BTN_HEIGHT           (BTN_WIDTH * KEYRATIO)         //按键的高
#define ITEM_HEIGHT          (BTN_HEIGHT + 10)              //item的高

#define TOTAL_HEIGHT         (ITEM_HEIGHT * 4 + 10)


@interface ZQKeyBoardView()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,ZQKeyBoardCellDelegate>
@property(nonatomic, assign)ZQKeyBoardType keyBoardType;

@property (nonatomic,strong) NSMutableArray * modelArray;



@property (nonatomic, strong)UICollectionView *topView;
@property (nonatomic, strong)UICollectionView *middleView;
@property (nonatomic, strong)UICollectionView *bottomView;


@property (nonatomic, strong)UIButton *uperButton;
@property (nonatomic, strong)UIButton *clearButton;


@property (nonatomic, strong)UIButton *numberClearButton;
@property (nonatomic, strong)UIButton *numberFinishedButton;


@property (nonatomic,strong) NSArray * letterArray;
@property (nonatomic, strong)NSArray * numberArray;
@property (nonatomic, strong)NSArray * symbolArray;

@property (nonatomic, assign)BOOL isUpper;

@end


@implementation ZQKeyBoardView

- (instancetype)initWithFrame:(CGRect)frame KeyBoardType:(ZQKeyBoardType)type{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.frame = CGRectMake(0, 0, SCREEN_WIDTH, TOTAL_HEIGHT + 10);
        self.keyBoardType = type;
        [self setData];
        [self setDataSourceModel];
        [self setupUI];
    }
    return self;
}
- (void)setData{
    self.numberArray  = @[@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"0"];
    self.letterArray = @[@"q",@"w",@"e",@"r",@"t",@"y",@"u",@"i",@"o",@"p",@"a",@"s",@"d",@"f",@"g",@"h",@"j",@"k",@"l",@"z",@"x",@"c",@"v",@"b",@"n",@"m"];
    self.symbolArray = @[@"@",@"*",@"#",@"%"];
}
- (void)setDataSourceModel{
    self.modelArray = [@[] mutableCopy];
    switch (self.keyBoardType) {
        case ZQKeyBoardTypeNumberAndLetter:{
            [self.numberArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                ZQKeyBoardModel * model = [ZQKeyBoardModel new];
                model.isUpper = YES;
                model.key = obj;
                [self.modelArray addObject:model];
            }];
            [self.letterArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                ZQKeyBoardModel * model = [ZQKeyBoardModel new];
                model.isUpper = YES;
                model.key = obj;
                [self.modelArray addObject:model];
            }];
        }
            break;
        case ZQKeyBoardTypeNumber:{
            [self.numberArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                ZQKeyBoardModel * model = [ZQKeyBoardModel new];
                model.isUpper = YES;
                model.key = obj;
                [self.modelArray addObject:model];
            }];
        }
            break;
        case ZQKeyBoardTypeNumberAndLetterAndSymbol:{
            [self.numberArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                ZQKeyBoardModel * model = [ZQKeyBoardModel new];
                model.isUpper = YES;
                model.key = obj;
                [self.modelArray addObject:model];
            }];
            [self.letterArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                ZQKeyBoardModel * model = [ZQKeyBoardModel new];
                model.isUpper = YES;
                model.key = obj;
                [self.modelArray addObject:model];
            }];
            [self.symbolArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                ZQKeyBoardModel * model = [ZQKeyBoardModel new];
                model.isUpper = YES;
                model.key = obj;
                [self.modelArray addObject:model];
            }];
        }
            break;
        default:
            break;
    }
}
- (void)setupUI{
    self.backgroundColor = [UIColor colorWithRed:245/255.0 green:245/255.0 blue:245/255.0 alpha:1.0];

    self.topView.backgroundColor = [UIColor clearColor];
    self.topView.delegate = self;
    self.topView.dataSource = self;
    [self.topView registerClass:[ZQKeyBoardCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([ZQKeyBoardCollectionViewCell class])];
   
    

    self.middleView.backgroundColor = [UIColor clearColor];
    self.middleView.delegate = self;
    self.middleView.dataSource = self;
    [self.middleView registerClass:[ZQKeyBoardCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([ZQKeyBoardCollectionViewCell class])];
    
    
    
    
    self.bottomView.backgroundColor = [UIColor clearColor];
    self.bottomView.delegate = self;
    self.bottomView.dataSource = self;
    [self.bottomView registerClass:[ZQKeyBoardCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([ZQKeyBoardCollectionViewCell class])];
   
    
    switch (self.keyBoardType) {
        case ZQKeyBoardTypeNumberAndLetter:{
            [self addSubview:self.topView];
            [self addSubview:self.middleView];
            [self addSubview:self.bottomView];
            [self addSubview:self.uperButton];
            [self addSubview:self.clearButton];
        }
            break;
        case ZQKeyBoardTypeNumber:{
            [self addSubview:self.topView];
            [self addSubview:self.middleView];
            [self addSubview:self.numberClearButton];
            [self addSubview:self.numberFinishedButton];
        }
            break;
        case ZQKeyBoardTypeNumberAndLetterAndSymbol:{
            [self addSubview:self.topView];
        }
            break;
            
            
        default:
            break;
    }
    
    [self changeFrame];


}

#pragma mark - UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    switch (self.keyBoardType) {
        case ZQKeyBoardTypeNumberAndLetter:{
            if (collectionView == self.topView)
            {
                return 2;
            }
            
            if (collectionView == self.middleView)
            {
                return 1;
            }
        }
            break;
        case ZQKeyBoardTypeNumber:{
            if (collectionView == self.topView)
            {
                return 3;
            }
        }
            break;
        case ZQKeyBoardTypeNumberAndLetterAndSymbol:{
            if (collectionView == self.topView)
            {
                return 4;
            }
        }
            break;
        default:
            break;
    }
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    switch (self.keyBoardType) {
        case ZQKeyBoardTypeNumberAndLetter:{
            if (collectionView == self.topView){
                return 10;
            }else if (collectionView == self.middleView){
                return 9;
            }else{
                return 7;
            }
        }
            break;
        case ZQKeyBoardTypeNumber:{
            if (collectionView == self.topView){
                return 3;
            }else{
                return 1;
            }
        }
            break;
        case ZQKeyBoardTypeNumberAndLetterAndSymbol:{
            return 10;
        }
            break;

        default:
            break;
    }
    return 10;
}

- (void)changeFrame{
    switch (self.keyBoardType) {
        case ZQKeyBoardTypeNumberAndLetter:{
            self.topView.frame = CGRectMake(0, 0, SCREEN_WIDTH, ITEM_HEIGHT * 2 + 2);
            self.middleView.frame = CGRectMake(SCREEN_WIDTH / 20.0, ITEM_HEIGHT *2, SCREEN_WIDTH, ITEM_HEIGHT);
            self.bottomView.frame = CGRectMake(SCREEN_WIDTH *3 / 20.0, ITEM_HEIGHT*3, SCREEN_WIDTH *9/10, ITEM_HEIGHT);
        }
            break;
        case ZQKeyBoardTypeNumber:{
            self.topView.frame = CGRectMake(0, 0, SCREEN_WIDTH, ITEM_HEIGHT *3);
            self.middleView.frame = CGRectMake(SCREEN_WIDTH/3, ITEM_HEIGHT *3, SCREEN_WIDTH/3, ITEM_HEIGHT);
        }
            break;
        case ZQKeyBoardTypeNumberAndLetterAndSymbol:{
            self.topView.frame = CGRectMake(0, 0, SCREEN_WIDTH, ITEM_HEIGHT *4);
        }
            break;
            
            
        default:
            break;
    }
    
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    //重用cell
    ZQKeyBoardCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([ZQKeyBoardCollectionViewCell class]) forIndexPath:indexPath];

    cell.delegate = self;
    NSInteger index;
    if (self.keyBoardType == ZQKeyBoardTypeNumber) {
        index = indexPath.section * 3 + indexPath.item;
    }else{
        index = indexPath.section * 10 + indexPath.item;
    }
    
    if (collectionView == self.middleView)
    {
        if (self.keyBoardType == ZQKeyBoardTypeNumber) {
            index = 9 + indexPath.section * 10 + indexPath.item;
        }else{
            index = 20 + indexPath.section * 10 + indexPath.item;
        }
    }
    else if (collectionView == self.bottomView)
    {
        index = 29 + indexPath.section * 10 + indexPath.item ;
    }
    cell.tag = index + 100;
    ZQKeyBoardModel * model = (ZQKeyBoardModel *)[self.modelArray objectAtIndex:index];
    ZQKeyBoardModel * newmodel = [ZQKeyBoardModel new];
    newmodel.key = model.key;
    newmodel.isUpper = YES;
    cell.model = newmodel;
    return cell;
}

#pragma mark - 设置每个UICollectionView最小垂直间距

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}

#pragma mark - 设置每个UICollectionView最小水平间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.keyBoardType == ZQKeyBoardTypeNumber) {
        return CGSizeMake(SCREEN_WIDTH / 3 - 6 , BTN_HEIGHT);
    }else{
        return CGSizeMake(BTN_WIDTH , BTN_HEIGHT);
    }
    
}


-(UICollectionView *)topView{
    if (!_topView) {
        CustomFlowLayout * flowLayout = [[CustomFlowLayout alloc]init];
        flowLayout.sectionInset = UIEdgeInsetsMake(10, 3, 0, 3);
        [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
        _topView = [[UICollectionView alloc]initWithFrame:self.bounds collectionViewLayout:flowLayout];
    }
    return _topView;
}



-(UICollectionView *)middleView{
    if (!_middleView) {
        CustomFlowLayout * flowLayout = [[CustomFlowLayout alloc]init];
        flowLayout.sectionInset = UIEdgeInsetsMake(10, 3, 0, 3);
        [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
        _middleView = [[UICollectionView alloc]initWithFrame:self.bounds collectionViewLayout:flowLayout];
    }
    return _middleView;
}

-(UICollectionView *)bottomView{
    if (!_bottomView) {
        CustomFlowLayout * flowLayout = [[CustomFlowLayout alloc]init];
        flowLayout.sectionInset = UIEdgeInsetsMake(10, 3, 0, 3);
        [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
        _bottomView = [[UICollectionView alloc]initWithFrame:self.bounds collectionViewLayout:flowLayout];
    }
    return _bottomView;
}

- (UIButton *)uperButton{
    if (!_uperButton) {
        _uperButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _uperButton.frame = CGRectMake(5, self.bounds.size.height - 10 - BTN_HEIGHT - 10, BTN_HEIGHT, BTN_HEIGHT);
        [_uperButton setImage:[UIImage imageNamed:@"key_upper_iphone"] forState:UIControlStateNormal];
        [_uperButton setImage:[UIImage imageNamed:@"key_upper_iphone_selected"] forState:UIControlStateSelected];
        [_uperButton addTarget:self action:@selector(respondToUperButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _uperButton;
}
- (UIButton *)clearButton{
    if (!_clearButton) {
        _clearButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _clearButton.frame = CGRectMake(SCREEN_WIDTH - 3 - BTN_HEIGHT, self.bounds.size.height - 10 - BTN_HEIGHT - 10, BTN_HEIGHT, BTN_HEIGHT);
        [_clearButton setImage:[UIImage imageNamed:@"key_delete_iphone"] forState:UIControlStateNormal];
        [_clearButton setImage:[UIImage imageNamed:@"key_delete_iphone_highlighted"] forState:UIControlStateHighlighted];
        [_clearButton addTarget:self action:@selector(respondToUperButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _clearButton;
}

- (UIButton *)numberClearButton{
    if (!_numberClearButton) {
        _numberClearButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _numberClearButton.frame = CGRectMake(1, self.bounds.size.height - 10 - ITEM_HEIGHT , SCREEN_WIDTH / 3 - 6 , BTN_HEIGHT);
        [_numberClearButton setTitle:@"取消" forState:UIControlStateNormal];
        _numberClearButton.backgroundColor = [UIColor redColor];
        _numberClearButton.layer.cornerRadius = 10.0;
        _numberClearButton.clipsToBounds = YES;
        [_numberClearButton addTarget:self action:@selector(respondToNumberClearButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _numberClearButton;
}
- (UIButton *)numberFinishedButton{
    if (!_numberFinishedButton) {
        _numberFinishedButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _numberFinishedButton.frame = CGRectMake(SCREEN_WIDTH - SCREEN_WIDTH/3 + 3, self.bounds.size.height - BTN_HEIGHT - 10 - 10, SCREEN_WIDTH / 3 -6 , BTN_HEIGHT);
        [_numberFinishedButton setTitle:@"完成" forState:UIControlStateNormal];
        _numberFinishedButton.backgroundColor = [UIColor redColor];
        _numberFinishedButton.layer.cornerRadius = 10.0;
        _numberFinishedButton.clipsToBounds = YES;
        [_numberFinishedButton addTarget:self action:@selector(respondToNumberFinishedButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _numberFinishedButton;
}


- (void)respondToUperButton:(UIButton *)sender{
    sender.userInteractionEnabled = NO;
    sender.selected = !sender.selected;
    self.isUpper = sender.selected;
    [self.topView reloadData];
    [self.middleView reloadData];
    [self.bottomView reloadData];
    sender.userInteractionEnabled = YES;
}
- (void)respondToClearButton:(UIButton *)sender{
    
}
- (void)respondToNumberFinishedButton:(UIButton *)sender{
    
}
- (void)respondToNumberClearButton:(UIButton *)sender{
    
}

@end

ZQKeyBoardModel.h

//
//  ZQKeyBoardModel.h
//  GSCB
//
//  Created by 赵前 on 2019/4/16.
//  Copyright © 2019年 赵前. All rights reserved.
//

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface ZQKeyBoardModel : NSObject


@property (nonatomic,copy) NSString * key;

@property (nonatomic,assign) BOOL isUpper;

@end

NS_ASSUME_NONNULL_END

ZQKeyBoardModel.m

//
//  ZQKeyBoardModel.m
//  GSCB
//
//  Created by 赵前 on 2019/4/16.
//  Copyright © 2019年 赵前. All rights reserved.
//

#import "ZQKeyBoardModel.h"

@implementation ZQKeyBoardModel

@end

CustomFlowLayout.h

//
//  CustomFlowLayout.h
//  GSCB
//
//  Created by 赵前 on 2019/4/16.
//  Copyright © 2019年 赵前. All rights reserved.
//

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface CustomFlowLayout : UICollectionViewFlowLayout

@end

NS_ASSUME_NONNULL_END

CustomFlowLayout.m

//
//  CustomFlowLayout.m
//  GSCB
//
//  Created by 赵前 on 2019/4/16.
//  Copyright © 2019年 赵前. All rights reserved.
//

#import "CustomFlowLayout.h"

@implementation CustomFlowLayout

- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
    NSMutableArray * attributes = [[super layoutAttributesForElementsInRect:rect] mutableCopy];
    
    //从第二个循环到最后一个
    for (NSInteger i = 1 ; i < attributes.count ; i ++ )
    {
        //当前的attribute
        UICollectionViewLayoutAttributes * currentLayoutAttributes = attributes[i];
        
        //上一个attribute
        UICollectionViewLayoutAttributes * prevLayoutAttributes = attributes[i - 1];
        
        //设置的最大间距,根绝需要修改
        CGFloat maximumSpacing = 6.0;
        
        //前一个cell的最右边
        CGFloat origin = CGRectGetMaxX(prevLayoutAttributes.frame);
        
        //如果当前一个cell的最右边加上我们的想要的间距加上当前cell的宽度依然在contentSize中,我们改变当前cell的原点位置
        //不加这个判断的后果是,UICollectionView只显示一行,原因是下面所有的cell的x值都被加到第一行最后一个元素的后面了
        if (origin + maximumSpacing + currentLayoutAttributes.frame.size.width < self.collectionViewContentSize.width)
        {
            CGRect frame = currentLayoutAttributes.frame;
            frame.origin.x = origin + maximumSpacing;
            currentLayoutAttributes.frame = frame;
        }
        
    }
    
    return attributes;
}
@end

ZQKeyBoardCollectionViewCell.h

//
//  ZQKeyBoardCollectionViewCell.h
//  GSCB
//
//  Created by 赵前 on 2019/4/16.
//  Copyright © 2019年 赵前. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "ZQKeyBoardModel.h"

NS_ASSUME_NONNULL_BEGIN

@protocol ZQKeyBoardCellDelegate <NSObject>

@optional

- (void)KeyBoardCellBtnClick:(NSInteger)Tag;


@end

@interface ZQKeyBoardCollectionViewCell : UICollectionViewCell



@property (nonatomic,weak) id <ZQKeyBoardCellDelegate> delegate;

@property (nonatomic,strong) UIButton * keyboardBtn;

@property (nonatomic,strong) ZQKeyBoardModel * model;



@end

NS_ASSUME_NONNULL_END

ZQKeyBoardCollectionViewCell.m

//
//  ZQKeyBoardCollectionViewCell.m
//  GSCB
//
//  Created by 赵前 on 2019/4/16.
//  Copyright © 2019年 赵前. All rights reserved.
//

#import "ZQKeyBoardCollectionViewCell.h"

@implementation ZQKeyBoardCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        [self setup];
    }
    return self;
}

- (void)setup
{
    [self keyboardBtn];
}

- (UIButton *)keyboardBtn
{
    if (!_keyboardBtn)
    {
        _keyboardBtn = [UIButton new];
        [_keyboardBtn setBackgroundImage:[UIImage imageNamed:@"keyboard_key"] forState:UIControlStateNormal];
        [_keyboardBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_keyboardBtn addTarget:self action:@selector(KeyboardBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        _keyboardBtn.layer.borderColor = [UIColor lightGrayColor].CGColor;
        _keyboardBtn.layer.borderWidth = 0.5f;
        [self.contentView addSubview:_keyboardBtn];
        
        _keyboardBtn.frame = self.contentView.bounds;
    }
    return _keyboardBtn;
}

- (void)KeyboardBtnClick:(UIButton *)sender
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(KeyBoardCellBtnClick:)])
    {
        [self.delegate KeyBoardCellBtnClick:self.tag - 100];
    }
}

- (void)setModel:(ZQKeyBoardModel *)model
{
    if (_model != model)
    {
        _model = model;
        
        if(!model.isUpper && self.tag > 9 + 100)
        {
            NSString * string = [model.key lowercaseString];
            
            [self.keyboardBtn setTitle:string forState:UIControlStateNormal];
        }
        else
        {
            [self.keyboardBtn setTitle:model.key forState:UIControlStateNormal];
        }
    }
}

@end

说明

代码中的点击大小写切换以及点击事件都还没完成。有这个思路,代码是参照某位码友的博客,改进之后可以使用三种类型的键盘,当然也可以自己添加。

如果有空,会把这个代码补全。这个自定义键盘只是提供一个思路,有些键盘还需要加密的设置。具体怎么加密,后面用到的时候我会更新到这里。

希望对大家有帮助。

最后

上传下几个图片,我也是找不到素材,有些按钮才没做

ios如何 自定义键盘ios如何 自定义键盘ios如何 自定义键盘ios如何 自定义键盘