How to add a note on UIwebview

727 views Asked by At

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()"]; 
}
1

There are 1 answers

0
binary_falcon On

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()"];