从 uiwebview Xcode 中获取选定的文本 [英] get selected text from a uiwebview Xcode

查看:22
本文介绍了从 uiwebview Xcode 中获取选定的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a UIWebView that loads text from an htmlString. I need when the user selects a part of the text and presses a button, i will be capable of extracting it in order to use it elsewhere, so i am using this code :

  // 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);
}

结果我总是一无所获......警报视图什么也没显示......这段代码有什么问题?有什么帮助吗?有任何想法吗 ?将不胜感激.

I always get nothing as a result ... The alert view shows nothing...What's the problem with this code ? Any help ? Any ideas ? It will be really appreciated.

推荐答案

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

The solution was actually pretty simple and no need for all the above code! For any future users just use:

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

这篇关于从 uiwebview Xcode 中获取选定的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆