manicaesar
manicaesar

Reputation: 5054

Getting selected text when UIMenuController is shown

When the user selects some text in UITextView or UIWebView, UIMenuController is shown. Is there any way to access this selected text programmitically?

I need it because I want to add custom item to UIMenuController: 'Search' option which will be intended to search for selected text in database.

Is it possible without using 'Copy' item in order to copy the text to pasteboard and then getting it from UIPasteBoard with the next time? - I am not interested in such workaround.

Upvotes: 2

Views: 2778

Answers (3)

RyanTCB
RyanTCB

Reputation: 8224

To grab text from a PDF displayed in a UIWebView this will work

 -(void)copiedString{

    [[UIApplication sharedApplication] sendAction:@selector(copy:) to:nil from:self forEvent:nil];
    NSString *copiedString =  [UIPasteboard generalPasteboard].string;
    NSLog(@"Copied String: %@",copiedString);
}

Upvotes: 1

Anh
Anh

Reputation: 6533

What I did was to add this method to a UIWebView's category:

- (NSString *)selectedText {
    return [self stringByEvaluatingJavaScriptFromString:@"window.getSelection().toString()"];
}

After that, I can use [webView selectedText] to access the current selection.

Upvotes: 5

Aditya
Aditya

Reputation: 4396

Did you have a look at Apple's documentation.You can use the MenuItems property provided to create your custom UIMenuController.Have a look at the image below

alt text

and follow this

link for details.

You can also have a look at the sample code provided by Apple to understand that feature.

http://developer.apple.com/library/ios/#samplecode/TableViewUpdates/Introduction/Intro.html#//apple_ref/doc/uid/DTS40010139

In this sample code they have show "Email" in the UIMenuController.

So go ahead and code...All the best...

Cheers

Upvotes: 1

Related Questions