Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom iPhone Keyboard

I need (i.e. a customer requirement) to provide a custom keyboard for the user to type text into both text fields and areas. I already have something that does the keyboard and appends test to a text field, however I'd like to make it more generic and have it act like the standard iphone keyboard, i.e. appear when teh user selects an editable text control. Currently my controller knows the target and the target is uneditable to prevent the standard keyboard.

Is there a way to hook into the behavior of text controls so I use my own keyboard easily?

Thanks, Vic

like image 570
vickirk Avatar asked Oct 22 '09 23:10

vickirk


People also ask

Can you make a custom keyboard on iPhone?

With iOS 8, Apple has made it possible to create custom keyboards that will be able to be used system wide in other apps. You can now ship a custom keyboard with your app and users will be able to choose it as the keyboard to use for every app that requires text input.


3 Answers

Here's an idea: modify the existing keyboard to your own needs. First, register to be notified when it appears on screen:

[[NSNotificationCenter defaultCenter] addObserver:self 
                                      selector:@selector(modifyKeyboard:)
                                      name:UIKeyboardWillShowNotification
                                      object:nil];

Then, in your modifyKeyboard method:

- (void)modifyKeyboard:(NSNotification *)notification 
{
    UIView *firstResponder = [[[UIApplication sharedApplication] keyWindow] performSelector:@selector(firstResponder)];

    for (UIWindow *keyboardWindow in [[UIApplication sharedApplication] windows])
        for (UIView *keyboard in [keyboardWindow subviews])
            if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES)
            {
                MyFancyKeyboardView *customKeyboard = [[MyFancyKeyboardView alloc] initWithFrame: CGRectMake(0, 0, keyboard.frame.size.width, keyboard.frame.size.height);
                [keyboard addSubview: customKeyboard];
                [customKeyboard release];
            }
}

This adds your view on top of the original keyboard, so make sure you make it opaque.

like image 96
luvieere Avatar answered Nov 04 '22 06:11

luvieere


You can do it with iOS 3.2 or later. Check the inputView property of UITextField for details.

like image 31
dwery Avatar answered Nov 04 '22 08:11

dwery


As long as you're not submitting your app to the app store, you can use a technique called method swizzling to dynamically replace methods of core classes at runtime. For example:

@interface UIControl(CustomKeyboard)
- (BOOL)__my__becomeFirstResponder
@end

@implementation UIControl(CustomKeyboard)
- (BOOL)__my__becomeFirstResponder
{
    BOOL becameFirstResponder = [self __my__becomeFirstResponder];
    if ([self canBecomeFirstResponder]) {
        [MyKeyboard orderFront];
    }
    return becameFirstResponder;
}

+ (void)initialize
{
    Method old = class_getInstanceMethod(self, @selector(becomeFirstResponder));
    Method new = class_getInstanceMethod(self, @selector(__my__becomeFirstResponder));
    method_exchangeImplementations(old, new);
}
@end

Please don't use anything like this in any production code. Also, I haven't actually tested this, so YMMV.

like image 43
ianh Avatar answered Nov 04 '22 06:11

ianh