의 텍스트를로드하는 UIWebView
이 있습니다. 사용자가 텍스트의 일부를 선택하고, 버튼 i는 다른 곳에서 사용하기 위해 그것을 압축 해제 할 것이다 누르면 제가 필요하므로이 코드를 사용하고 : 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);
}
항상 결과로 아무 것도 얻지 않습니다. 경고보기에는 아무 것도 표시되지 않습니다.이 코드의 문제점은 무엇입니까? 어떤 도움이 필요합니까? 어떤 아이디어? 정말 감사하겠습니다.
당신의 기여에 감사드립니다 ...하지만 1 년 전에 대답을 얻었습니다 !!! 여기에 엄지 손가락이 있습니다 :) –
webview content에서 선택한 텍스트의 시작과 끝 범위를 얻는 방법 –