本文将从0开始构建一个输入法项目,包括中英文切换,手写,文字联想,9宫格数字键盘

先列出需要注意的点

1.需要将项目和键盘项目的target的版本设置为测试机所适配的最低版本,否则运行不了

2.手机下面有按键的和没有按键的不一样,有按键的会有生成一个含有next keyboard的文字按钮,我们需要删除,没有按键的下面会自动生成地球按钮,用来切换键盘

一.创建项目

1.直接新建一个输入法的项目,可以添加一个UITextView的控件来实现吊起键盘

UITextView *textField = [[UITextView alloc]init];
    self.textView = textField;
    textField.frame = CGRectMake(([UIScreen mainScreen].bounds.size.width-200)/2,    ([UIScreen mainScreen].bounds.size.height-200)/2, 200, 200);
    textField.text = @"测试输入法";
    textField.layer.borderColor = [UIColor blackColor].CGColor;
    textField.layer.borderWidth = 0.5;
    [self.view addSubview:textField];
    CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height;
    NSLog(@"screenHeight的值为:%f",screenHeight);

2.可以让点击除了控件之外的其他区域额,让其收回键盘,取消第一响应者也可以,或者让view停止编辑

//收回键盘
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
//    [self.textView resignFirstResponder];
}

二、创建输入法target

1.创建一个Custom Keyboard Extension的target

iOS开发 keywindow上的输入框 ios编程输入法_控件

2.创建完成之后点击Activate

iOS开发 keywindow上的输入框 ios编程输入法_键盘高度_02

 3.在手机设置里面添加创建好的键盘就可以了

iOS开发 keywindow上的输入框 ios编程输入法_ios_03

三、全键盘页面进行布局 

1.首先把创建好项目之后的KeyboardViewController里面的nextButton给取消掉,不然在有按钮的手机下面会多一个显示

iOS开发 keywindow上的输入框 ios编程输入法_输入法_04

2. 适配不同机型的键盘,此时是按iphone5手机来计算出一个键盘高度与手机屏幕高度的比例

/// 自适应键盘高度
/// @param height 键盘高度
- (void)prepareHeightConstraint:(CGFloat)height {
    if (self.heightConstraint == nil) {
        self.heightConstraint = [NSLayoutConstraint constraintWithItem:self.view attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0.0 constant:height];
    } else {
        self.heightConstraint.constant = height;
    }
    [self.view addConstraint:self.heightConstraint];
}

然后在viewWillAppear方法里面调用此方法传入高度

- (void)viewWillAppear:(BOOL)animated {
    NSLog(@"%s",__func__);
    [super viewWillAppear:animated];
    //自适应键盘的高度,按照iphone5键盘和手机的比例来进行计算
    CGFloat height = SCREEN_HEIGHT * (216.0/568.0);
    [self prepareHeightConstraint:height];
}

3.需要注意的是不管是收回键盘还是从其他的键盘切换到自己的键盘,都会走viewDidLoad方法,所以在我们调用库初始化的时候一定要注意内存问题,我这里初始化库的时候用的单例

   

三、接下来进行新建键盘项目,开始键盘的设置

1.创建键盘项目

iOS开发 keywindow上的输入框 ios编程输入法_控件

创建完成好之后输入项目名称,出现如下弹框,选择 Activate

iOS开发 keywindow上的输入框 ios编程输入法_ios_06

 2.创建完成好之后我们可以看一下自己的目录结构

iOS开发 keywindow上的输入框 ios编程输入法_控件_07

3.此时我们就可以在手机设置通用里面添加自己设置的键盘。

iOS开发 keywindow上的输入框 ios编程输入法_ios_08

 

iOS开发 keywindow上的输入框 ios编程输入法_控件_09

 

iOS开发 keywindow上的输入框 ios编程输入法_控件_10

4.键盘设置好之后我们运行项目切换自己的键盘就可以

 

iOS开发 keywindow上的输入框 ios编程输入法_ios_11

四、进行键盘选项的布局和全键盘布局及联想功能。

1.创建键盘选项布局

KeyboardViewController设置
- (void)viewDidLoad {
    [super viewDidLoad];
    [self configUI];
}
- (void)configUI {
    
    CGFloat toolBarHeight = 40;
    CCToolBar *toolBar = [[CCToolBar alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, toolBarHeight)];
    toolBar.delegate = self;
    self.toolBar = toolBar;
    [self.view addSubview:toolBar];
}
2.CCToolBar设置
 CCToolBar.h文件
typedef NS_ENUM(NSUInteger, CCTopBarAction) {
    CCTopBarActionVoice = 0,        //语音
    CCTopBarActionSwitchBoard,  //切换键盘
    CCTopBarActionRecommend,    //推荐语句
    CCTopBarActionQuit          //退出键盘
};
@protocol CCToolBarDelegate <NSObject>
- (void)didClickToolBar:(CCTopBarAction)action;
@end
@interface CCToolBar : UIView
@property (nonatomic, weak) id <CCToolBarDelegate>delegate;
@end
CCToolBar.m文件
#import "CCToolBar.h"
@implementation CCToolBar
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self configUI];
    }
    return self;
}
- (void)configUI {
    NSArray *imageNames = @[@"toolbar_voice_20x20_",
                            @"toolbar_keyboardMode_20x20_",
                            @"toolbar_translate_20x20_",
                            @"toolbar_dismiss_20x20_"];
    self.backgroundColor = [UIColor colorWithRed:189/255.0 green:189/255.0 blue:196/255.0 alpha:1];
    CGSize buttonSize = CGSizeMake(self.frame.size.width/imageNames.count, self.frame.size.height);
    for (NSInteger i = 0; i<imageNames.count; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(i*buttonSize.width, 0, buttonSize.width, buttonSize.height);
        [button setImage:[UIImage imageNamed:imageNames[i]] forState:UIControlStateNormal];
        button.tag = i;
        [button addTarget:self action:@selector(didClickButton:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:button];
    }
    
}
- (void) didClickButton:(UIButton *)sender {
    CCTopBarAction action = (CCTopBarAction)(sender.tag);
    if ([self.delegate respondsToSelector:@selector(didClickToolBar:)]) {
        [self.delegate didClickToolBar:action];
    }
    
}

在键盘项目中创建图片资源需要新建一个Asset文件,将图片放入到这个文件夹当中

iOS开发 keywindow上的输入框 ios编程输入法_键盘高度_12

 实现的效果如下

iOS开发 keywindow上的输入框 ios编程输入法_控件_13

好了,今天先写到这,明天继续更新,如果有问题欢迎私信