iOS8新特性扩展(Extension)应用之四——自定义键盘控件

iOS8新特性扩展(Extension)应用之四——自定义键盘控件

        iOS8系统的开放第三方键盘,使得用户在输入法的选择上更加自主灵活,也更加贴近不一样语言的输入风格。这篇博客,将介绍如何开发一个第三方的键盘控件。浏览器

1、了解UIInputViewController类

        UIInputViewController是系统扩展支持键盘扩展的一个类,经过这个类,咱们能够自定义一款咱们本身的键盘提供给系统使用。ide

        首先,咱们先来看一下这个类中的一些属性和方法:布局

@property (nonatomic, retain) UIInputView *inputView;atom

键盘的输入视图,咱们能够自定义这个视图。spa

@property (nonatomic, readonly) NSObject <UITextDocumentProxy> *textDocumentProxy;.net

实现了UITextDocumentProxy协议的一个对象,后面会介绍这个协议。code

@property (nonatomic, copy) NSString *primaryLanguage;orm

系统为咱们准备了一些本地化的语言字符串对象

- (void)dismissKeyboard;继承

收键盘的方法

- (void)advanceToNextInputMode;

切换到下一输入法的方法

UITextDocumentProxy协议内容以下:

@protocol UITextDocumentProxy <UIKeyInput>
//输入的上一个字符
@property (nonatomic, readonly) NSString *documentContextBeforeInput;
//即将输入的一个字符
@property (nonatomic, readonly) NSString *documentContextAfterInput;
//将输入的字符移动到某一位置
- (void)adjustTextPositionByCharacterOffset:(NSInteger)offset;

@end

而UITextDocumentProxy这个协议继承与UIKeyInput协议,UIKeyInput协议中提供的两个方法用于输入字符和删除字符:

- (void)insertText:(NSString *)text;
- (void)deleteBackward;

2、建立一款最简单的数字输入键盘

        建立一个项目,做为宿主APP,接着咱们File->new->target->customKeyBoard:

系统要求咱们对键盘的布局要使用autolayout,而且只能够采用代码布局的方式,咱们这里为了简单演示,将坐标写死:

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 设置数字键盘的UI
    //数字按钮布局
    for (int i=0; i<10; i++) {
        UIButton * btn = [UIButton buttonWithType:UIButtonTypeSystem];
        btn.frame=CGRectMake(20+45*(i%3), 20+45*(i/3), 40, 40);
        btn.backgroundColor=[UIColor greenColor];
        [btn setTitle:[NSString stringWithFormat:@"%d",i] forState:UIControlStateNormal];
        btn.tag=101+i;
        [btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:btn];
    }
    //建立切换键盘按钮
    UIButton * change = [UIButton buttonWithType:UIButtonTypeSystem];
    change.frame=CGRectMake(200,20, 80, 40) ;
    NSLog(@"%f,%f",self.view.frame.size.height,self.view.frame.size.width);
    [change setBackgroundColor:[UIColor blueColor]];
    [change setTitle:@"切换键盘" forState:UIControlStateNormal];
    [change addTarget:self action:@selector(change) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:change];
    //建立删除按钮
    UIButton * delete = [UIButton buttonWithType:UIButtonTypeSystem];
    delete.frame=CGRectMake(200, 120, 80, 40);
    [delete setTitle:@"delete" forState:UIControlStateNormal];
    [delete setBackgroundColor:[UIColor redColor]];
    [delete addTarget:self action:@selector(delete) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:delete];
}
//删除方法
-(void)delete{
    if (self.textDocumentProxy.documentContextBeforeInput) {
        [self.textDocumentProxy deleteBackward];
    }
}
//切换键盘方法
-(void)change{
    [self advanceToNextInputMode];
}
//点击数字按钮 将相应数字输入
-(void)click:(UIButton *)btn{
    [self.textDocumentProxy insertText:[NSString stringWithFormat:@"%ld",btn.tag-101]];
}

运行后,在使用以前,咱们须要先加入这个键盘:在模拟器系统设置中general->keyboard->keyboards->addNowKeyboard

选中咱们自定义的键盘,以后运行浏览器,切换到咱们的键盘,效果以下:

        

专一技术,热爱生活,交流技术,也作朋友。

——珲少 QQ群:203317592

相关文章
相关标签/搜索