如何从UIWebView获取选择文本?

时间:2022-08-25 00:05:41

, Hello, everyone.

, 大家好。

I'm developing my app which has a feature to post article from website.

我正在开发我的应用程序,它具有从网站发布文章的功能。

This is a picture of UIWebview with UIMenuController.

这是UIWebview与UIMenuController的图片。

如何从UIWebView获取选择文本?



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.

当用户点击按钮时,可以获得事件。但我找不到获取用户所选文本的方法。

In UITextView case, it has 'selectedRange' property, so it is easy to get selection text.

在UITextView案例中,它具有'selectedRange'属性,因此很容易获得选择文本。

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];

I would like to get the selection text on UIWebView. Does anybody have an idea or hint?

我想在UIWebView上获取选择文本。有人有想法或暗示吗?

Thanks.

1 个解决方案

#1


13  

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:

或者如果你不想搞乱Javascript,你可以将选择复制到粘贴板,然后通过运行检索它:

    [[UIApplication sharedApplication] sendAction:@selector(copy:) to:nil from:self forEvent:nil];
    NSString *text =  [UIPasteboard generalPasteboard].string;

#1


13  

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:

或者如果你不想搞乱Javascript,你可以将选择复制到粘贴板,然后通过运行检索它:

    [[UIApplication sharedApplication] sendAction:@selector(copy:) to:nil from:self forEvent:nil];
    NSString *text =  [UIPasteboard generalPasteboard].string;