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

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

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

一、了解UIInputViewController类

        UIInputViewController是系统扩展支持键盘扩展的一个类,通过这个类,我们可以自定义一款我们自己的键盘提供给系统使用。

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

@property (nonatomic, retain) UIInputView *inputView;

键盘的输入视图,我们可以自定义这个视图。

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

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

@property (nonatomic, copy) NSString *primaryLanguage;

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

- (void)dismissKeyboard;

收键盘的方法

- (void)advanceToNextInputMode;

切换到下一输入法的方法

UITextDocumentProxy协议内容如下:

?
1
2
3
4
5
6
7
8
9
@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;

二、创建一款最简单的数字输入键盘

        创建一个项目,作为宿主APP,接着我们File->new->target->customKeyBoard:

105006_AFk6_2340880.png

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
- ( 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

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

105628_dKoe_2340880.png

        

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值