# Custom Keyboard

# Custom KeyBoard Example

Objective-C and Xib

Add a target to an existing XCode project

enter image description here (opens new window)

In the Add Target select Custom KeyBoardenter image description here (opens new window)

Add the target like this:

enter image description here (opens new window)

Your project file directory should look something like this

enter image description here (opens new window)

Here myKeyBoard is the name of the added Target

Add new Cocoatouch file of type of type UIView and add an interface file

enter image description here (opens new window)

Finally your project directory should look like this

enter image description here (opens new window)

make the keyBoardView.xib a subclass of keyBoardView

enter image description here (opens new window)

Make interface in the keyBoardView.xib file

enter image description here (opens new window)

Make connections to from the keyBoardView.xib to keyBoardView.h file

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 a colloection of all the keys '0 to 9' '+' '-' and '.'

@end

In the keyBoardViewController.h file import #import "keyBoardView.h"

Declare a property for keyboard @property (strong, nonatomic)keyBoardView *keyboard;

Comment out the

@property (nonatomic, strong) UIButton *nextKeyboardButton and all the code associated with it

The KeyboardViewController.m file's viewDidLoad() function should look like this

- (void)viewDidLoad {
    [super viewDidLoad];
    self.keyboard=[[[NSBundle mainBundle]loadNibNamed:@"keyBoardView" owner:nil options:nil]objectAtIndex:0];
      self.inputView=self.keyboard;
    [self addGestureToKeyboard];

    // Perform custom UI setup here
//    self.nextKeyboardButton = [UIButton buttonWithType:UIButtonTypeSystem];
//    
//    [self.nextKeyboardButton setTitle:NSLocalizedString(@"Next Keyboard", @"Title for 'Next Keyboard' button") forState:UIControlStateNormal];
//    [self.nextKeyboardButton sizeToFit];
//    self.nextKeyboardButton.translatesAutoresizingMaskIntoConstraints = NO;
//    
//    [self.nextKeyboardButton addTarget:self action:@selector(advanceToNextInputMode) forControlEvents:UIControlEventTouchUpInside];
//    
//    [self.view addSubview:self.nextKeyboardButton];
//    
//    [self.nextKeyboardButton.leftAnchor constraintEqualToAnchor:self.view.leftAnchor].active = YES;
//    [self.nextKeyboardButton.bottomAnchor constraintEqualToAnchor:self.view.bottomAnchor].active = YES;
}

The functions addGestureToKeyboard, pressDeleteKey, 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 Main Application and go to Settings->General->Keyboard->Add New Keyboard-> and add keyboard from the third party keyboard section (The displayed keyboardName would be keyBoardCustom)

The keyboard name can be changed by adding a key called Bundle display name and in the Value String Value enter the desired name for the keyboard of the main Project.

enter image description here (opens new window)

You can also watch this Youtube Video (opens new window)