如何在UIwebview上添加注释

发布于 2024-10-18 16:36:33 字数 349 浏览 5 评论 0原文

我想实现一个功能,例如像 iBooks 和 Amazon Kindle 那样在 UIWebview 上添加所选文本的注释。

我已经创建了 UIMenuItem。但是,我不知道如何实现这个方法。有人可以在这方面提供帮助吗?

我不知道这个方法是用来实现什么功能的。谢谢。

- (void)Note:(id)sender {
    NSString *selection = [webView stringByEvaluatingJavaScriptFromString:@"window.getSelection().toString()"]; 
}

I would like to implement a function like adding a note of a selected text on UIWebview as iBooks and Amazon Kindle does.

I already created UIMenuItem. But, I dont know how to implement the method for this. Could anybody help in this regard?

I dont know what functionality is used to implement in this method. Thanks.

- (void)Note:(id)sender {
    NSString *selection = [webView stringByEvaluatingJavaScriptFromString:@"window.getSelection().toString()"]; 
}

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

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

发布评论

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

评论(1

合久必婚 2024-10-25 16:36:33

相关 JavaScript 旨在为您提供在您的选择中突出显示的文本。

但我不确定在元素上运行 toString 会返回正确的信息。以下方法将返回突出显示的文本并将其保存到变量中。

function getHighlightedString() {
    var text        = window.getSelection();
    myAnchorOffset = text.anchorOffset;
    myFocusOffset = text.focusOffset;
    myHighlightLength=myFocusOffset-myAnchorOffset;
    if(myHighlightLength<0)
    {
        myHighlightLength*=-1;
        temp = myAnchorOffset;
        myAnchorOffset = myFocusOffset;
        myFocusOffset = temp;

    }


    selectedText    = text.anchorNode.textContent.substr(myAnchorOffset, myFocusOffset - myAnchorOffset);



}

当你将此方法加载到 webview 中时

NSString myHighlightedText = [webView stringByEvaluatingJavaScriptFromString:@"getHighlightedString()"];

The JavaScript in question is designed to give you the text that is highlighted within your selection.

I'm not sure that running toString on an element will return the correct information though. The following method will return the highlighted text and save it into a variable.

function getHighlightedString() {
    var text        = window.getSelection();
    myAnchorOffset = text.anchorOffset;
    myFocusOffset = text.focusOffset;
    myHighlightLength=myFocusOffset-myAnchorOffset;
    if(myHighlightLength<0)
    {
        myHighlightLength*=-1;
        temp = myAnchorOffset;
        myAnchorOffset = myFocusOffset;
        myFocusOffset = temp;

    }


    selectedText    = text.anchorNode.textContent.substr(myAnchorOffset, myFocusOffset - myAnchorOffset);



}

when you have this method loaded into the webview

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