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 ti开发者_JS百科me? - I am not interested in such workaround.
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.
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
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
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);
}
精彩评论