如何从UIWebView获取选择文本? [英] How to get selection text from UIWebView?
问题描述
,大家好。
我正在开发我的应用程序,该应用程序具有从网站发布文章的功能。
I'm developing my app which has a feature to post article from website.
这是UIWebview与UIMenuController的图片。
This is a picture of UIWebview with UIMenuController.
当用户点击按钮时可能会出现事件。但我无法找到获取用户所选文本的方法。
It was possible to get event when user tap the button. but I can't find the way to get the text the user selected.
在UITextView案例中,它具有'selectedRange'属性,因此很容易获得选择文本。
In UITextView case, it has 'selectedRange' property, so it is easy to get selection text.
UIMenuItem *facebookMenuItem = [[UIMenuItem alloc] initWithTitle:@"Facebook" action:@selector(facebookMenuItemTapped:)];
UIMenuItem *twitterMenuItem = [[UIMenuItem alloc] initWithTitle:@"Twitter" action:@selector(twitterMenuItemTapped:)];
[UIMenuController sharedMenuController].menuItems = [NSArray arrayWithObjects: facebookMenuItem, twitterMenuItem, nil];
[twitterMenuItem release];
[facebookMenuItem release];
我想在UIWebView上获取选择文本。
有没有人有想法或提示?
I would like to get the selection text on UIWebView. Does anybody have an idea or hint?
谢谢。
推荐答案
或者如果你不想搞乱Javascript,你可以将选择复制到粘贴板然后通过运行来检索它:
Or if you donʾt want to mess with Javascript you can just copy the selection to the pasteboard and then retrieve it by running:
[[UIApplication sharedApplication] sendAction:@selector(copy:) to:nil from:self forEvent:nil];
NSString *text = [UIPasteboard generalPasteboard].string;
这篇关于如何从UIWebView获取选择文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!