wagashi
wagashi

Reputation: 894

iOS how to simulate two taps on uitextview programmatically?

I want to Do this, Because I want to grab the selected text after tapping once. If there is another approach to select text, please let me know.

Thank you.


I modified Luiz solution, but it copies whole text on textview. What I want to achieve is to select one word.

    if (gestureRecognizer.state == UIGestureRecognizerStateRecognized)
    {
        textview.selectedRange = NSMakeRange(0, textview.text.length);
        NSString *selected = [self.textview.text substringWithRange:textview.selectedRange];
        NSLog (@"selected = %@", selected);

    }

EDIT:

I am still working to find a well-working code for this task. In big apps like Kindle.app and Wakaru.app, it is possible to tap once and force selecting one word like two taps would do. Do you know how to do this or an idea to achieve this function?

Upvotes: 2

Views: 1413

Answers (3)

tarmes
tarmes

Reputation: 15442

Having read all your comments, I think that I now understand your question to be: "In a UITextView how can I have one tap select text in a the same way that a double tap would normally?"

I don't think you'll find a solution to this other than writing your own text view by hand.

Upvotes: 1

Try following these steps:

  1. Set an IBOutlet to your UITextView, called, let's say, myTextView.

  2. On viewDidLoad of your view controller, create an UITapGestureRecognizer for myTextView:

    UITapGestureRecognizer *tapTextView = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
    [myTextView addGestureRecognizer:tapTextView];
    [tapTextView release];
    
  3. Implement the tap method so it makes the UITextView selects its text:

     - (void)tap:(UITapGestureRecognizer *)recognizer
     {
         if (recognizer.state == UIGestureRecognizerStateRecognized) {
             myTextView.selectedRange = NSMakeRange(0, myTextView.text.length);
         }
     }
    

I hope this helps!

Best regards!

Upvotes: 2

tarmes
tarmes

Reputation: 15442

You're question is unclear.

When you say "If there is another approach to select text, please let me know", it seems that you just need to set the selectedRange property of the UITextView.

On the other hand, I don't see the link with double tapping....

Tim

Upvotes: 1

Related Questions