Custom keyboard example
suggest changeObjective-C and Xib
Add a target to an existing Xcode project.
In the target template list, select Custom Keyboard.
Add the target like this:
The project should look something like this.
Here myKeyBoard is the name of the added Target
Add a Cocoa Touch class that subclasses UIView, and create a XIB for it.
The project should then look like this.
Set the XIB’s custom class to keyBoardView.
Make interface in the keyBoardView.xib file
Connect the XIB controls to keyBoardView.h.
keyBoardView.h should look like
#import <UIKit/UIKit.h>
@interface keyBoardView : UIView
@property (weak, nonatomic) IBOutlet UIButton *deleteKey;
//IBOutlet for the delete Key
@property (weak, nonatomic) IBOutlet UIButton *globe;
//Outlet for the key with title globe which changes the keyboard type
@property (strong, nonatomic) IBOutletCollection(UIButton) NSArray *keys;
// Contains the digit keys, plus '+', '-', and '.'
@end
In KeyboardViewController.h, import keyBoardView.h and declare the view. Remove the template’s nextKeyboardButton property. The globe key calls advanceToNextInputMode instead.
#import "keyBoardView.h"
@property (strong, nonatomic) keyBoardView *keyboard;
loadNibNamed: must use the extension bundle. mainBundle is the host app and does not contain this XIB. Add the loaded view to self.view. UIInputViewController’s inputView is a UIInputView, not this UIView subclass.
- (void)viewDidLoad {
[super viewDidLoad];
NSBundle *bundle = [NSBundle bundleForClass:[self class]];
self.keyboard = [[bundle loadNibNamed:@"keyBoardView" owner:nil options:nil] firstObject];
self.keyboard.frame = self.view.bounds;
self.keyboard.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view addSubview:self.keyboard];
[self addGestureToKeyboard];
}
addGestureToKeyboard, pressDeleteKey, and keyPressed are defined below.
-(void) addGestureToKeyboard
{
[self.keyboard.deleteKey addTarget:self action:@selector(pressDeleteKey) forControlEvents:UIControlEventTouchUpInside];
[self.keyboard.globe addTarget:self action:@selector(advanceToNextInputMode) forControlEvents:UIControlEventTouchUpInside];
for (UIButton *key in self.keyboard.keys)
{
[key addTarget:self action:@selector(keyPressed:) forControlEvents:UIControlEventTouchUpInside];
}
}
-(void) pressDeleteKey
{
[self.textDocumentProxy deleteBackward];
}
-(void)keyPressed:(UIButton *)key
{
[self.textDocumentProxy insertText:[key currentTitle]];
}
Run the containing app, then open Settings → General → Keyboard → Keyboards → Add New Keyboard and choose this keyboard under third-party keyboards. With this sample target, the name shown is keyBoardCustom.
Set Bundle display name (CFBundleDisplayName) on the keyboard extension target, not on the main app, to change that name.
You can also watch this Youtube Video