How to get selection text from UIWebView?(如何从 UIWebView 获取选择文本?)
问题描述
,大家好.
我正在开发我的应用程序,该应用程序具有从网站发布文章的功能.
I'm developing my app which has a feature to post article from website.
这是带有 UIMenuController 的 UIWebview 的图片.
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 获取选择文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何从 UIWebView 获取选择文本?
基础教程推荐
- Play 商店的设备兼容性问题 2022-01-01
- Xcode UIView.init(frame:) 只能在主线程中使用 2022-01-01
- 为什么姜饼模拟器方向卡在应用程序中? 2022-01-01
- navigationItem.backBarButtonItem 不工作?为什么上一个菜单仍然显示为按钮? 2022-01-01
- UIImage 在开始时不适合 UIScrollView 2022-01-01
- Android Volley - 如何动画图像加载? 2022-01-01
- iOS - UINavigationController 添加多个正确的项目? 2022-01-01
- 如何比较两个 NSDate:哪个是最近的? 2022-01-01
- 如何将图像从一项活动发送到另一项活动? 2022-01-01
- SwiftUI-ScrollViewReader的ScrollTo不滚动 2022-01-01
