我有一个带有一些基本 html 的 UIWebView,它充当一个简单的编辑器。
// in initWithFrame: of UIView subclass.
_editorHTML = [@"<!doctype html>"
"<html>"
"<head>"
"<meta name=\"viewport\" content=\"width=device-width,initial-scale=1\">"
"<style type=\"text/css\">"
"#content { font-family:Arial, Helvetica, sans-serif; font-size:1em; } "
"</style>"
"<script type=\"text/javascript\">"
"function load()"
"{"
" window.location.href = 'ready://' + document.body.offsetHeight;"
"}"
"</script>"
"</head>"
"<body id=\"bodyDiv\" onload=\"load()\">"
"<div id=\"content\" contenteditable=\"true\">%@</div>"
"</body>"
"</html>" retain];
_webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
_webView.delegate = self;
_webView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
_webView.scrollView.bounces = NO;
[_webView loadHTMLString:[NSString stringWithFormat:_editorHTML, @""] baseURL:[NSURL fileURLWithPath:directory]];
- (BOOL) webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL *url = [request URL];
if (navigationType == UIWebViewNavigationTypeOther) {
if ([[url scheme] isEqualToString:@"ready"]) {
int height = (int)_webView.frame.size.height;
[webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.getElementById('content').style.minHeight = '%dpx'", height - WEBVIEW_DIVHEIGHT_OFFSET]];
return NO;
}
}
return YES;
}
那么,如何确定我的 div 中的光标位置,以便我可以更新我的 scrollView 以确保内容在 UIKeyboard 上方可见,从而不必让用户手动滚动以保持文本可见?