1

Am working for am message based iPhone app. In my application i have loaded the message content in UITextView and added an UIImage on UITextView.

Now i want to select all UITextView content by holding UITextView and show the Copy option to the user. Currently when the user hold UITextView some of the content only selecting.

Any one please help me to do this? Thanks in advance.

EDIT:

In UITableView CellForRowAtIndexPath delegate

    customMessageTextView = [[MessageTextView alloc] initWithFrame:CGRectZero];
    customMessageTextView.tag = 100;
    UIFont *font = [UIFont fontWithName:@"Helvetica" size:15]; 
    customMessageTextView.font = font;
    customMessageTextView.scrollEnabled = NO;
    customMessageTextView.delegate = self;
    customMessageTextView.dataDetectorTypes = UIDataDetectorTypeLink;
    [cell.contentView addSubview:customMessageTextView];
    [customMessageTextView sizeToFit]; 

    for (UIGestureRecognizer *recognizer in customMessageTextView.gestureRecognizers) 
    {
        if ([recognizer isKindOfClass:[UILongPressGestureRecognizer class]])
        {
            recognizer.enabled = NO;
        }
    }


    UILongPressGestureRecognizer *myLongPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(selectAllTextFromCustomMessageTextView)]; 
    [customMessageTextView addGestureRecognizer:myLongPressRecognizer];
    [myLongPressRecognizer release];

UILongPressGestureRecognizer action:

-(void) selectAllTextFromCustomMessageTextView
{
    NSLog(@"Select All Text Messages");
    customMessageTextView.selectedRange = NSMakeRange(0, customMessageTextView.text.length);
}
Gopinath
  • 5,392
  • 21
  • 64
  • 97

1 Answers1

2

If I understand you correctly you want to disable the standard behaviour when holding in a UITextView (i.e. the magnifying glass, etc.). Perhaps you have even disabled the editing option. If so you should just add an UILongPressGestureRecognizer to your UITextView. You might have to disable the UILongPressGestureRecognizer which is built into UITextView by default. You can find a way to do that here.

Then in your UILongPressGestureRecognizer action method you simply select all the text in the view:

[textView selectAll:self];

Notice that this will bring up the Copy/Cut/Paste menu. However, if your text view does in fact have user editing disabled the menu will only contain Copy.

Community
  • 1
  • 1
pajevic
  • 4,607
  • 4
  • 39
  • 73
  • Thanks for your answer. I am trying your instructions. I will let you know the result. Thanks again. – Gopinath Sep 13 '12 at 09:25
  • It is not working for me. I will post my code. Could you please help me? Thanks. – Gopinath Sep 13 '12 at 10:00
  • You are right, setting the `selectedRange` property does not work. I have updated my answer with a method that should work (I have tested it this time). – pajevic Sep 13 '12 at 11:46
  • 1
    that selectall: does not work, neither selectrange it must be for something else, who knows please post. – Boris Gafurov Feb 21 '14 at 19:40