IOS8 new feature extension (Extension) application four--Custom keyboard controls
IOS8 System Open third-party keyboard, so that users in the choice of input method more independent and flexible, but also closer to the input style of different languages. This blog will show you how to develop a third-party keyboard control.
first, understand the Uiinputviewcontroller class
Uiinputviewcontroller is a class of system extensions that support keyboard extensions, and through this class, we can customize our own keyboard for the system to use.
First, let's take a look at some of the properties and methods in this class:
@property (nonatomic, retain) Uiinputview *inputview;
Input view of the keyboard, we can customize this view.
@property (nonatomic, readonly) nsobject <UITextDocumentProxy> *textdocumentproxy;
Implements an object for the Uitextdocumentproxy protocol, which is described later in this article.
@property (nonatomic, copy) NSString *primarylanguage;
The system prepares some localized language strings for us
-(void) Dismisskeyboard;
How to collect the keyboard
-(void) Advancetonextinputmode;
To switch to the next input method
The contents of the Uitextdocumentproxy agreement are as follows:
@protocol uitextdocumentproxy <uikeyinput>//Input Previous character @property (Nonatomic, readonly) NSString * documentcontextbeforeinput;//a character that will be entered @property (Nonatomic, readonly) NSString *documentcontextafterinput;// Moves the entered character to a position-(void) Adjusttextpositionbycharacteroffset: (Nsinteger) offset; @end
and Uitextdocumentproxy This agreement inherits with Uikeyinput agreement, Uikeyinput the two methods provided in the protocol are used to enter characters and delete characters:
-(void) InsertText: (NSString *) text;
-(void) Deletebackward;
Second, create a simple digital input keyboard
Create a project as the hosting app, and then we File->new->target->customkeyboard:
The system requires us to use AutoLayout for the layout of the keyboard, and can only be used in the way of code layout, we here for a simple demonstration, the coordinates are written dead:
- (void) viewdidload { [super viewdidload]; // Set the ui //Digital button layout for the numeric keypad 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]; } //Create toggle Keyboard button 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:@ "Toggle Keyboard" Forstate:uicontrolstatenormal]; [change addtarget:self action: @selector ( Change) forcontrolevents:uicontroleventtouchupinside]; [self.view addsubview: change]; //Create Delete button 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];} Delete Method-(void) delete{ if (self.textDocumentProxy.documentContextBeforeInput) { [self.textDocumentProxy deleteBackward]; }}//Switch Keyboard Method-(void) change{ [self advancetonextinputmode];} Click the Number button Enter the corresponding number-(void) Click: (uibutton *) btn{ [self.textdocumentproxy inserttext:[nsstring stringwithformat:@ "%ld", btn.tag-101]];}
After running, before using, we need to join this keyboard: in the simulator system Setup General->keyboard->keyboards->addnowkeyboard
Check our custom keyboard, then run the browser, switch to our keyboard, the effect is as follows:
IOS8 new feature extension (Extension) application four--Custom keyboard controls