如何在iOS中获得突出显示的WebView内容的完整范围?
我突出显示了WebView内容,并没有突出显示文本。当我在WebView内容中长按并拖动时,我能够选择特定的单词,该时间区域被选择为未高亮显示 但现在的问题是,当我触摸WebView颜色文本中的任何位置时,我希望获得完整的颜色跨度如何在iOS中获得突出显示的WebView内容的完整范围?,ios,objective-c,rangy,Ios,Objective C,Rangy,我突出显示了WebView内容,并没有突出显示文本。当我在WebView内容中长按并拖动时,我能够选择特定的单词,该时间区域被选择为未高亮显示 但现在的问题是,当我触摸WebView颜色文本中的任何位置时,我希望获得完整的颜色跨度 请帮帮我。我之前写过以下代码片段,可能对您有用 // this function only check if the selection is spanned across the highlighted span function shouldSelectText(
请帮帮我。我之前写过以下代码片段,可能对您有用
// this function only check if the selection is spanned across the highlighted span
function shouldSelectText() {
// http://stackoverflow.com/a/6056743/510577
var sel = rangy.getSelection();
if (sel.rangeCount) {
var range = sel.getRangeAt(0);
var nodes = range.getNodes([1], function(node) {
return node.tagName.toLowerCase() == "span"; // only need to satisfy one of open / close "span" tag, so no need to use "containsNode"
});
return (nodes.length <= 0);
}
return false;
}
shouldSelectText
返回true,则可以使用selectHighlight
展开所选内容下的突出显示// the return value indicates whether it is highlighted before
function selectHighlight() {
var sel = rangy.getSelection();
if (sel.rangeCount > 0) {
// console.log("has selection!");
if (_highlighter.getHighlightsInSelection().length <= 0) {
return false; // do nothing if it is not highlighted before
}
// else the selection is highlighted, so we expand it to select the whole highlight
window.requestAnimationFrame(function() {
var range = rangy.createRange();
range = _highlighter.getHighlightsInSelection()[0].getRange(); // use non-disclosed private API of rangy highlighter!
sel.setSingleRange(range);
_highlightUnderSelection = _highlighter.getIntersectingHighlights(rangy.getSelection().getAllRanges())[0]; // must be the first one
});
return true;
}
return false;
}
另外,您需要使用
UIWebView
的stringByEvaluatingJavaScriptFromString
从本机调用上述JS我之前已经编写了以下代码片段,可能对您有用
// this function only check if the selection is spanned across the highlighted span
function shouldSelectText() {
// http://stackoverflow.com/a/6056743/510577
var sel = rangy.getSelection();
if (sel.rangeCount) {
var range = sel.getRangeAt(0);
var nodes = range.getNodes([1], function(node) {
return node.tagName.toLowerCase() == "span"; // only need to satisfy one of open / close "span" tag, so no need to use "containsNode"
});
return (nodes.length <= 0);
}
return false;
}
shouldSelectText
返回true,则可以使用selectHighlight
展开所选内容下的突出显示// the return value indicates whether it is highlighted before
function selectHighlight() {
var sel = rangy.getSelection();
if (sel.rangeCount > 0) {
// console.log("has selection!");
if (_highlighter.getHighlightsInSelection().length <= 0) {
return false; // do nothing if it is not highlighted before
}
// else the selection is highlighted, so we expand it to select the whole highlight
window.requestAnimationFrame(function() {
var range = rangy.createRange();
range = _highlighter.getHighlightsInSelection()[0].getRange(); // use non-disclosed private API of rangy highlighter!
sel.setSingleRange(range);
_highlightUnderSelection = _highlighter.getIntersectingHighlights(rangy.getSelection().getAllRanges())[0]; // must be the first one
});
return true;
}
return false;
}
另外,您需要使用
UIWebView
的stringByEvaluatingJavaScriptFromString
从本机调用上述JS您是如何实现WebView突出显示功能的?是通过将JavaScript发送到底层HTML来改变它的风格吗?如果是的话,你可以考虑使用库来实现它。是的,我正在使用javascript获取所选文本范围的起点和终点。我也使用了rangy。但我的客户说,当我触摸高亮显示的内容中的任何单词时,我希望选择完整的彩色文本,而不是特定的单词。我如何能做到这一点?但在这个rangy中,无法使用未高亮显示的功能。var removeHighlightFromSelectedText=函数(事件){if(highlighttexttoberemove){highlighter.removeHighlights([highlighttexttoberemove]);var containts={text:highlighter.serialize()};highlighttexttoberemove='';};此方法无效。您是如何实现WebView突出显示功能的?是通过将JavaScript发送到底层HTML来改变它的风格吗?如果是的话,你可以考虑使用库来实现它。是的,我正在使用javascript获取所选文本范围的起点和终点。我也使用了rangy。但我的客户说,当我触摸高亮显示的内容中的任何单词时,我希望选择完整的彩色文本,而不是特定的单词。我如何能做到这一点?但在这个rangy中,无法使用未高亮显示的功能。var removeHighlightFromSelectedText=函数(事件){if(highlighttexttoberemove){highlighter.removeHighlights([highlighttexttoberemove]);var containts={text:highlighter.serialize()};highlighttexttoberemove='';};这种方法行不通。