2012-11-08 11 views
5

htmlStringからテキストを読み込むUIWebViewがあります。 私は、ユーザーがテキストの一部を選択し、ボタンを押したときに、私は他の場所でそれを使用するためには、それを抽出することができるであろう必要があるので、私はこのコードを使用しています:HighlightedString.jsとともにuiwebviewから選択したテキストを取得するXcode

// 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]; 

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

私は結果として何も得られません...アラートビューには何も表示されません...このコードの問題点は何ですか?どんな助け?何か案は ?本当に感謝します。

答えて

14

実際には、ソリューションは非常にシンプルで、上記のコードは必要ありませんでした。将来のユーザーの場合 だけ使用します。

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

これは、文字列変数に、あなたの選択を通過します。

+1

あなたの貢献に感謝します...しかし、私は答えを1年前に得ました!ここをクリックしてください:) –

+0

WebViewコンテンツで選択したテキストの開始と終了の範囲を取得する方法 –

関連する問題