Search code examples
iosobjective-cnsstringuigesturerecognizersnapchat

Display keyboard on UIImage with UIGestureRecognizer and use NSString


I would like to display the keyboard when I tap on the image and use the keyboard for displaying letters I've touched and stock them in a variable NSString. (like Snapchat). How can I display the letters in "live" ?

I've made UIGestureRecognizer and recognizer for the location where I've tapped. Here is my code:

-(void)tapDetected:(UIGestureRecognizer*)recognizer{
NSLog(@"tap detected.");
CGPoint point = [recognizer locationInView:self.truckImageView];
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10, point.y, 300, 40)];
[textField becomeFirstResponder];
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.font = [UIFont systemFontOfSize:15];
textField.placeholder = @"enter text";
textField.autocorrectionType = UITextAutocorrectionTypeNo;
textField.keyboardType = UIKeyboardTypeDefault;
textField.returnKeyType = UIReturnKeyDone;
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
textField.delegate = self;
[self.view addSubview:textField];
NSLog(@"x = %f y = %f", point.x, point.y );

Solution

  • There's no way to show up UIKeyboard with UIImage officially.

    When user touches the image, add UITextField at the point and set the firstResponder. You don't even need to use drawText:.