gpt4 book ai didi

javascript - 特定的 UIWebView 选择(仅整个单词/句子)

转载 作者:搜寻专家 更新时间:2023-10-31 08:37:14 24 4
gpt4 key购买 nike

我希望用户只能在我的 UIWebView 中选择整个句子。我在 UIWebView 中使用我自己的 UIMenuItem(一个书签按钮)。我要保存这个书签(核心数据)并想确定突出显示的句子/诗句。我以编程方式布置 html(从 sqlite 数据库构建),并有一个 <span id="x"> (其中 x 是一个可变整数)包围每个句子。我知道 Amazon Kindle 应用程序只允许用户选择整个单词。我该怎么做?

谢谢!

最佳答案

您是否尝试过setMarkedText:selectedRange:

Apple developer lib

更新:

虽然您不能在UIWebView 中使用setMarkedText,但除了使用JavaScript 别无他法。我不知道,如果您可以操作您正在显示的 HTML 页面?您可以在页面中添加此脚本。如果您不能操作页面,您应该在加载实际页面的 UIWebView 中加载一个 iframe,并在 iframe 之后添加此脚本。

这是脚本:

document.addEventListener('mouseup', function(){
if(getSelection().anchorNode != null ){
var sel = getSelection(),
range = sel.getRangeAt(0),
nodeValue = sel.anchorNode.nodeValue,
lastCharIndex = range.endOffset,
firstCharIndex = range.startOffset,
lastChar = nodeValue.substr(lastCharIndex, 1),
firstChar = nodeValue.substr(firstCharIndex, 1);

while(lastChar != (" " || ".")){
lastChar = nodeValue.substr(lastCharIndex, 1);
lastCharIndex++;
};

while(firstChar != " "){
firstChar = nodeValue.substr(firstCharIndex, 1);
firstCharIndex--;
};

range.setEnd(sel.anchorNode, lastCharIndex-1);
sel.addRange(range);
range.setStart(sel.anchorNode, firstCharIndex+2);
sel.addRange(range);
}
}, false);

我在我的 iPhone 上测试过它,它工作正常。

这是 Demo (只需选择一些东西)。我花了很多时间在上面。我希望你喜欢它。

关于javascript - 特定的 UIWebView 选择(仅整个单词/句子),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7001705/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com