Custom iPhone Keyboard

vickirk picture vickirk · Oct 23, 2009 · Viewed 11.1k times · Source

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

Answer

luvieere picture luvieere · Oct 23, 2009

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.