从uiwebview Xcode中获取所选文本

时间:2012-11-08 08:56:27

标签: text uiwebview xcode4.3

我有一个UIWebView来加载htmlString的文字。 我需要当用户选择文本的一部分并按下按钮时,我将能够提取它以便在其他地方使用它,所以我使用此代码:

  // The JS File   
NSString *filePath  = [[NSBundle mainBundle] pathForResource:@"HighlightedString" ofType:@"js" inDirectory:@""];
NSData *fileData    = [NSData dataWithContentsOfFile:filePath];
NSString *jsString  = [[NSMutableString alloc] initWithData:fileData encoding:NSUTF8StringEncoding];
[WebV2 stringByEvaluatingJavaScriptFromString:jsString];

// The JS Function
NSString *startSearch   = [NSString stringWithFormat:@"getHighlightedString()"];
[WebV2 stringByEvaluatingJavaScriptFromString:startSearch];

NSString *selectedText   = [NSString stringWithFormat:@"selectedText"];
NSString * highlightedString = [WebV2 stringByEvaluatingJavaScriptFromString:selectedText];

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Highlighted String" 
                                                message:highlightedString
                                               delegate:nil 
                                      cancelButtonTitle:@"Oh Yeah" 
                                      otherButtonTitles:nil];
[alert show];

HighlightedString.js

一起
/*!
 ------------------------------------------------------------------------ 
 // Search Highlighted String
 ------------------------------------------------------------------------ 
 */
var selectedText = "";

function getHighlightedString() {
    var text        = window.getSelection();
    selectedText    = text.anchorNode.textContent.substr(text.anchorOffset, text.focusOffset - text.anchorOffset);

}

// ...
function stylizeHighlightedString() {

    var range               = window.getSelection().getRangeAt(0);
    var selectionContents   = range.extractContents();
    var span                = document.createElement("span");

    span.appendChild(selectionContents);

    span.setAttribute("class","uiWebviewHighlight");
    span.style.backgroundColor  = "black";
    span.style.color            = "white";

    range.insertNode(span);
}


// helper function, recursively removes the highlights in elements and their childs
function uiWebview_RemoveAllHighlightsForElement(element) {
    if (element) {
        if (element.nodeType == 1) {
            if (element.getAttribute("class") == "uiWebviewHighlight") {
                var text = element.removeChild(element.firstChild);
                element.parentNode.insertBefore(text,element);
                element.parentNode.removeChild(element);
                return true;
            } else {
                var normalize = false;
                for (var i=element.childNodes.length-1; i>=0; i--) {
                    if (uiWebview_RemoveAllHighlightsForElement(element.childNodes[i])) {
                        normalize = true;
                    }
                }
                if (normalize) {
                    element.normalize();
                }
            }
        }
    }
    return false;
}

// the main entry point to remove the highlights
function uiWebview_RemoveAllHighlights() {
    selectedText = "";
    uiWebview_RemoveAllHighlightsForElement(document.body);
}

我总是得不到任何结果......警报视图没有显示任何内容......此代码有什么问题?有帮助吗?有任何想法吗 ?非常感谢。

2 个答案:

答案 0 :(得分:14)

解决方案实际上非常简单,不需要上述所有代码! 对于任何未来的用户,只需使用:

 NSString *textToSpeech = [WebV2 stringByEvaluatingJavaScriptFromString: @"window.getSelection().toString()"];
 NSLog(@" -**-*--****-*---**--*-* This is the new select text %@",[WebV2 stringByEvaluatingJavaScriptFromString: @"window.getSelection().toString()"] );

答案 1 :(得分:2)

NSString *theSelectedText = [self.webView stringByEvaluatingJavaScriptFromString:@"window.getSelection().toString()"];

这会将您的选择传递给字符串变量。