问题描述
我正在研究一个在UIWebView中显示PDF的项目.我已将另一个UIMenuItem添加到UIMenuController中,以便对已选择的UIWebView中的文本执行各种操作.问题是我无法在新创建的选择器方法中访问选定的文本.如果我先对选定的文本使用复制命令,则可以从粘贴板上获取先前复制的文本,但是可以使用[myWebView copy:sender]之类的命令;从我的新选择器调用不会执行任何操作.如何在新的选择器中接收选定的文本?我知道在UIWebView中使用HTML时可以很容易地用javascript做到这一点,人们通常如何使用UIWebView中显示的PDF文件来做到这一点?
I am working on a project that displays a PDF in a UIWebView. I have added another UIMenuItem to the UIMenuController in order to do various things with the text in the UIWebView that has been selected. The problem is that I am not able to access the selected text in my newly created selector method. If I use the copy command on selected text first, then I can get that previously copied text from the pasteboard, but a command like [myWebView copy:sender]; called from my new selector does nothing. How can I receive the selected text in my new selector? I know this can be done easily with javascript when working with HTML in a UIWebView, how do people usually do this with PDF files displayed in a UIWebView?
推荐答案
您可以使用以下代码从第一响应者调用复制命令:
You can call the copy command from the first responder using this code:
[[UIApplication sharedApplication] sendAction:@selector(copy:) to:nil from:self forEvent:nil];
然后,您可以简单地从粘贴板中检索它:
Then you can simply retrieve it from the pasteboard:
[UIPasteboard generalPasteboard].string;
这似乎是从显示PDF的UIWebView接收选定文本的唯一方法,javascript方法仅适用于HTML,不适用于PDF文件.
This seems to be the only way to receive the selected text from a UIWebView that is displaying a PDF, the javascript methods will not work with PDF files, only HTML.
这篇关于如何从UIWebView中显示的PDF复制所选数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!