Add text input to the iOS app

Don't have our View class implement the UIKeyInput protocol any
more. It won't work properly anyway. The docs say: "Only a small
subset of the available keyboards and languages are available to
classes that adopt this protocol".

Instead, use a transparent UITextView on top of our View to accept
keyboard input.

Seems to work as expected.

Change-Id: I3093ea7fbfa0ecab0dc5d0a38e5695723e8ed4ad
This commit is contained in:
Tor Lillqvist 2013-04-14 01:41:28 +03:00
parent 6b89688829
commit e3e040f671
4 changed files with 35 additions and 35 deletions

View File

@ -10,12 +10,13 @@
#import "View.h"
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@interface AppDelegate : UIResponder <UIApplicationDelegate, UITextViewDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) View *view;
- (void) threadMainMethod: (id) argument;
- (void)threadMainMethod: (id) argument;
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text;
@end

View File

@ -16,7 +16,7 @@
#import "lo.h"
static UIView *theView;
static View *theView;
@implementation AppDelegate
@ -41,6 +41,12 @@ static UIView *theView;
vc.view = self.view;
theView = self.view;
self.view->textView = [[UITextView alloc] initWithFrame: r];
self.view->textView.autocapitalizationType = UITextAutocapitalizationTypeNone;
self.view->textView.alpha = 0;
[self.view addSubview: self.view->textView];
self.view->textView.delegate = self;
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self.view action:@selector(tapGesture:)];
[self.window addGestureRecognizer: tapRecognizer];
@ -68,6 +74,17 @@ static UIView *theView;
}
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
NSLog(@"textView: %@ shouldChangeTextInRange:[%u,%u] replacementText:%@", textView, range.location, range.length, text);
assert(textView == theView->textView);
for (NSUInteger i = 0; i < [text length]; i++)
lo_keyboard_input([text characterAtIndex: i]);
return NO;
}
- (void)applicationWillResignActive:(UIApplication *)application
{
(void) application;
@ -101,16 +118,17 @@ static UIView *theView;
[[info objectForKey:UIKeyboardFrameBeginUserInfoKey] getValue:&frameBegin];
[[info objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&frameEnd];
NSLog(@"keyboardWillShow: frame:%dx%d@(%d,%d)",
(int) frameEnd.size.width, (int) frameEnd.size.height,
(int) frameEnd.origin.x, (int) frameEnd.origin.y);
}
- (void)keyboardDidHide:(NSNotification *)note
{
NSDictionary *info = [note userInfo];
CGRect frameBegin;
CGRect frameEnd;
(void) note;
[[info objectForKey:UIKeyboardFrameBeginUserInfoKey] getValue:&frameBegin];
[[info objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&frameEnd];
NSLog(@"keyboardDidHide");
lo_keyboard_did_hide();
}
@ -129,14 +147,14 @@ void lo_damaged(CGRect rect)
void lo_show_keyboard()
{
dispatch_async(dispatch_get_main_queue(), ^{
[theView becomeFirstResponder];
[theView->textView becomeFirstResponder];
});
}
void lo_hide_keyboard()
{
dispatch_async(dispatch_get_main_queue(), ^{
[theView resignFirstResponder];
[theView->textView resignFirstResponder];
});
}

View File

@ -9,8 +9,11 @@
#import <UIKit/UIKit.h>
@interface View : UIView <UIKeyInput>
@interface View : UIView
{
@public
UITextView* textView;
}
- (void)drawRect:(CGRect)rect;
- (void)tapGesture:(UIGestureRecognizer *)gestureRecognizer;

View File

@ -34,33 +34,11 @@
CGPoint location = [gestureRecognizer locationInView: self];
NSLog(@"tapGesture: at: (%d,%d)", (int)location.x, (int)location.y);
lo_tap(location.x, location.y);
[self->textView becomeFirstResponder];
} else
NSLog(@"tapGesture: %@", gestureRecognizer);
}
- (void)insertText:(NSString *)text
{
(void) text;
// Do something with the typed character
}
- (void)deleteBackward
{
// Handle the delete key
}
- (BOOL)hasText
{
// Return whether there's any text present
return YES;
}
- (BOOL)canBecomeFirstResponder
{
return YES;
}
@end
// vim:set shiftwidth=4 softtabstop=4 expandtab: