当内容可编辑的 UIWebView 位于空白行时,获取插入符 rect [英] Get caret rect in content-editable UIWebView when it's on a blank line
问题描述
出于某种原因,我需要在内容可编辑的 UIWebView 中获得插入符号的垂直位置.由于许多方法可以阻止插入符号闪烁(例如,在创建/删除隐藏节点时),我使用此 javascript/Objective-C 代码:
For some reason, I need to get the caret vertical position in a content editable UIWebView. As many methods to do that stop caret blinking (for example when creating/removing a hidden node), I use this javascript/Objective-C code:
- (CGRect)caretRect {
NSString *js = @"function f(){ var sel = document.getSelection(); var range = sel.getRangeAt(0).cloneRange(); range.collapse(true); var r =range.getClientRects()[0]; return '{{'+r.left+','+r.top+'},{'+r.width+','+r.height+'}}'; } f();";
NSString *result = [self.webTexteView stringByEvaluatingJavaScriptFromString:[NSString stringWithString:js]];
CGRect rect = CGRectFromString(result);
return rect;
}
不幸的是,当插入符号位于空行时,此代码返回一个 {0, 0} {0, 0} 矩形...
Unfortunately, when the caret is on a blank line, this code returns a {0, 0} {0, 0} rect...
有谁知道如何在任何情况下获得插入符矩形(以像素为单位列出垂直位置),同时保持闪烁?谢谢!
Does anybody knows how getting caret rect (at list the vertical position in pixels) in any circumstances, while keeping it blinking? Thanks!
推荐答案
这个答案为我解决了这个问题:https://stackoverflow.com/a/6847328/135712
This answer fixed this problem for me: https://stackoverflow.com/a/6847328/135712
基本上,您需要插入一个新的 DOM 元素,获取其位置,然后将其删除.
Basically, you need to insert a new DOM element, get its position, then remove it.
这篇关于当内容可编辑的 UIWebView 位于空白行时,获取插入符 rect的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!