Question

I just want to make my uitextview editable if the user makes a double tap on it.

But in my Code it only works once. After hiding the keyboard the double tap stops working like I want to. It only shows the "copy and define" popup, if I try it a second time...

Maybe the "textViewDidEndEditing" is not the right way for this...

Here is the Code:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    CGFloat screen_height = [UIScreen mainScreen].bounds.size.height;
    CGFloat screen_width  = [UIScreen mainScreen].bounds.size.width;

    scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0,0,screen_width,screen_height)];
    scrollView.showsVerticalScrollIndicator=NO;
    scrollView.scrollEnabled=NO;
    scrollView.userInteractionEnabled=YES;
    [scrollView setBackgroundColor:[UIColor colorWithRed: 0.0 green: 0.0 blue: 0.0 alpha: 0.5]];
    [self.view addSubview:scrollView];
    scrollView.contentSize = CGSizeMake(screen_width,screen_height);

    MyTextView = [[UITextView alloc]initWithFrame:CGRectMake(0, 0, screen_width, screen_height)];
    MyTextView.text = @"Headline\nNo1";
    MyTextView.textColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0];
    [MyTextView setFont:[UIFont fontWithName:@"Helvetica" size:90]];
    MyTextView.textAlignment = NSTextAlignmentCenter;
    MyTextView.delegate = self;
    [MyTextView setScrollEnabled:NO];
    [MyTextView setEditable:NO];
    [MyTextView setUserInteractionEnabled:YES];
    [MyTextView setBackgroundColor:[UIColor colorWithRed: 0.0 green: 0.0 blue: 0.0 alpha: 0.0]];
    [MyTextView sizeToFit];
    [scrollView addSubview:MyTextView];

    // TAP
    UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTap:)];
    [doubleTap setNumberOfTapsRequired:2];
    [doubleTap setNumberOfTouchesRequired:1];
    [MyTextView addGestureRecognizer:doubleTap];

    // KEYBOARD
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil];

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];
}


-(void)doubleTap:(UITapGestureRecognizer *)sender {

    NSLog(@"View: %@", [[sender view] class]);
    UITextView *tappedView = (UITextView *)[sender view];
    [tappedView setEditable:YES];
    [tappedView becomeFirstResponder];
}


-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {

    if ([gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]]) {

        if (((UITapGestureRecognizer *)gestureRecognizer).numberOfTapsRequired == 2) {
            return YES;
        }
    }
    return YES;
}


-(BOOL)canBecomeFirstResponder {

    if (MyTextView.editable == YES) {
        return YES;
    }
    else {
        return NO;
    }
}


- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGesture {

    return YES;
}


-(void)textViewDidEndEditing:(UITextView *)textView {

    [textView setEditable:NO];
}


-(void)keyboardDidHide:(NSNotification *)aNotification {

    NSLog(@"keyboardDidHide");

    // [textView setEditable:NO]; // same result like textViewDidEndEditing
}
Was it helpful?

Solution

Remove/comment [textView setEditable:NO]; from method
-(void)textViewDidEndEditing:(UITextView *)textView.

This is the only think that causing you trouble. because best way to stop editing mode of UITextView/UItextField is resignFirstReponder method call.

EDIT

For Hiding Keyboard add following lines in viewDidLoad method

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)];
[self.view addGestureRecognizer:singleTap];

Than add hideKeyboard as follows

-(void) hideKeyboard
{
    [MyTextView resignFirstResponder];
}

OTHER TIPS

Things will be simple if you add a clear view above the textview and add double tap gesture on the clear view . When double tap is recognized, remove the clear view and make textview become first responder

[clearView removeFromSuperview] ;
[textview becomeFirstResponder] ;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top