在 UIwebview 中突出显示并做笔记

发布于 2024-10-16 09:24:47 字数 213 浏览 3 评论 0原文

我正在为 ipad/iphone 开发一个电子书应用程序,它加载 epub 文件。我想知道如何突出显示具有背景颜色的文本,并在 UIWebView 而不是 UIView 中做类似于 iBook 或 Kindle 的笔记。我可以通过长按来拖动选择文本。但我只能看到“复制”选项。我想添加突出显示并添加注释到该菜单。如何用背景颜色突出显示文本?...我的文档已加载到 UIWebView 中。

谢谢。

I am deveoping an ebook app for ipad/iphone which loads epub file. I would like to know how to highlight a text with a background color and make notes similar to iBook or Kindle in UIWebView and NOT UIView. I am able to drag select the text by long tapping. But I can only see "copy" option. I would like to add highlight and add note to that menu. How to hightlight the text with a background color?... My document is loaded in UIWebView.

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

顾冷 2024-10-23 09:24:47

您需要做的是将注释和突出显示选项添加到共享菜单中。在您的视图控制器 viewdidload 中(或添加视图后的任何时间)。下面将两个具有两种选择器方法的菜单项添加到您的菜单中。

UIMenuItem *customMenuItem1 = [[UIMenuItem alloc] initWithTitle:@"Highlight" action:@selector(doHighlight:)];
UIMenuItem *customMenuItem2 = [[UIMenuItem alloc] initWithTitle:@"Note" action:@selector(doNote:)];
//[myArray addObject:customMenuItem2];
NSMutableArray *myArray = [[NSMutableArray alloc]initWithObjects:customMenuItem1,customMenuItem2, nil ] ;   
[UIMenuController sharedMenuController].menuItems = myArray;

您可能还想使用一些 JavaScript 来确定保存您的选择的矩形,然后在文本上覆盖半透明的 uiview。

因此,在您的 doNote 方法中,加载 javascript 并运行它,请参阅下面的 javascript 以获取您可能想要执行的操作的示例。

NSString *rectString = [webView stringByEvaluatingJavaScriptFromString:rectStringText];

下面的代码将在文本周围得到一个矩形,尽管它本身会比您的文本大:如果您突出显示了第 2 行的 char 10 到第 5 行的 char 50,它将返回一个突出显示第 2 行到 char 0 的矩形endline 第 5 行。

function selectEl(x,y){
document.designMode = "on";
var el = document.elementFromPoint(x, y);
var rect = el.getBoundingClientRect();
var vx = rect.left;
var width = rect.right - vx;

var height = rect.bottom - rect.top;
var vy = rect.top;

var range = document.createRange();
range.selectNodeContents(el);
var sel = document.getSelection();
sel.removeAllRanges();
sel.addRange(range);
document.designMode = "off";


rectString = '{{'+vx+','+vy+'},{'+width+','+height+'}}';
}

现在您有了一个矩形字符串,您可以从返回的字符串和 uiview 创建一个 CRect 以覆盖在您的 web 视图中。 [注意,您必须弄清楚如何处理缩放并保存此数据以供下次加载]

What you need to do is add the note and highlight options to your shared menu. In your viewcontroller viewdidload (or anytime after your view has been added). The following adds two menu items with two selector methods to your menu.

UIMenuItem *customMenuItem1 = [[UIMenuItem alloc] initWithTitle:@"Highlight" action:@selector(doHighlight:)];
UIMenuItem *customMenuItem2 = [[UIMenuItem alloc] initWithTitle:@"Note" action:@selector(doNote:)];
//[myArray addObject:customMenuItem2];
NSMutableArray *myArray = [[NSMutableArray alloc]initWithObjects:customMenuItem1,customMenuItem2, nil ] ;   
[UIMenuController sharedMenuController].menuItems = myArray;

You may also want to use a bit of javascript to determine the rect that holds your selection, then overlay a translucent uiview over the text.

So, within your doNote method, load up javascript and run it, see the below javascript for an example of what you may want to do.

NSString *rectString = [webView stringByEvaluatingJavaScriptFromString:rectStringText];

The following will get a rect around the text, though it will inherently be larger than your text: if you've highlighted at char 10 on line 2 through char 50 on line 5, it will return a rect that highlights char 0 line 2 through endline line 5.

function selectEl(x,y){
document.designMode = "on";
var el = document.elementFromPoint(x, y);
var rect = el.getBoundingClientRect();
var vx = rect.left;
var width = rect.right - vx;

var height = rect.bottom - rect.top;
var vy = rect.top;

var range = document.createRange();
range.selectNodeContents(el);
var sel = document.getSelection();
sel.removeAllRanges();
sel.addRange(range);
document.designMode = "off";


rectString = '{{'+vx+','+vy+'},{'+width+','+height+'}}';
}

So, now that you have a rectangle string, you can create a CRect from the string returned and uiview to overlay within your webview. [Note, you will have to figure out how to deal with zooming and saving this data for your next load]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文