[英]UIWebView “document.height” evaluation returns wrong height on older iphones (specifically 4s and 5)
[英]UIWebView's content height returns wrong value while using mathjax
我正在使用mathjax在UIWebView
上顯示數學表達式。 所以我想動態更改UIWebView
高度。 為此,我用了
CGFloat newHeight = [[self.questionWebView stringByEvaluatingJavaScriptFromString:@"document.documentElement.scrollHeight;"] floatValue];
在webViewDidFinishLoad:
委托方法。
這是我的問題,如果我使用正則表達式, UIWebView
高度將返回正確的值,如下圖所示,
如果我輸入包含/,(,)的表達式,它將給出錯誤的高度,如下圖所示
NSString *html = [NSString stringWithFormat:@"<html><head><title>Untitled</title><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" /><meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\" /><script type=\"text/x-mathjax-config\">MathJax.Hub.Config({extensions: [\"tex2jax.js\"],jax: [\"input/TeX\",\"output/HTML-CSS\"],tex2jax: {inlineMath: [[\"$\",\"$\"],[\"\\(\",\"\\)\"]]}});</script><script type=\"text/javascript\" src=\"../MathJax.js?config=AM_HTMLorMML-full\"></script></head><style>.Question {font-family:'Times New Roman',arial;font-weight:normal;font-size:12px;color:#323232;}</style><body><table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" class=\"Question\" align=\"left\" width=\"260\" height=\"40\"><tr><td> `x + 3 = 2` </td></tr></div></body></html>"];
[self writeStringToFile:tempDir fileName:@"test1.html" pathName:filePath content:html];
NSURLRequest* req = [[NSURLRequest alloc] initWithURL:url];
[self.myWebView loadRequest:req];
在writeStringToFile:
方法中,
-(void)writeStringToFile:(NSString *)dir fileName:(NSString *)strFileName pathName:(NSString *)strPath content:(NSString *)strContent{
NSString *path = [dir stringByAppendingPathComponent:strFileName];
NSString *foo0 = @"<html><head><meta name='viewport' content='initial-scale=1.0;maximum-scale=100.0;user-scalable=1'/>"
"<script type='text/javascript' src='";
NSString *foo1 = @"?config=AM_HTMLorMML-full'></script>"
"</head>"
"<body>";
NSString *foo2 = @"</body></html>";
NSString *fooFinal = [NSString stringWithFormat:@"%@%@%@%@%@",foo0,strPath,foo1,strContent,foo2];
[fooFinal writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil];
}
如何獲得准確的高度?
提前致謝!
問題是MathJax需要花費一些時間來執行其動作,並且在MathJax對數學進行排版之前(即,當它仍然只是一行文本時),您將獲得新的高度。 您將必須將新高度與MathJax的動作同步。 我對UIWebView編程一無所知,但是您可以讓MathJax通過包含以下內容來告訴您何時完成排版:
<script type="text/x-mathjax-config">
MathJax.Hub.Queue(function () {alert("done")});
</script>
在HTML中,該腳本在加載MathJax.js本身的腳本之前 。 當然,您需要用需要采取的任何操作來替換alert("done")
,以便與運行UIWebView的代碼進行通信(這是我不知道的部分)。 您可以從外部檢測事件或UIWebView內部值的更改嗎? 如果是這樣,那就是您要在排隊的函數中執行的操作,並且外部代碼將需要等待該操作才能更改高度。
最終,經過長時間的研究,我在@Davide Cervone答案和示例https://github.com/dannywartnaby/KPWebViewInterceptor的幫助下找到了解決方案,
剛剛在.html
文件中添加了Objective-C方法調用
<script type="text/x-mathjax-config">
MathJax.Hub.Queue(function myFunction() {
objc_msgsend('myFunction');
});
</script>
在.js
文件中
var KP_INTERCEPTOR_INVOCATION_COMMAND = "invoke"
var KP_INTERCEPTOR_SEPERATOR = ":"
function objc_msgsend() {
var selector = arguments[0];
var invocation = selector;
if ( arguments.length > 1 ) {
for( var i = 1; i < arguments.length; i++ ) {
invocation = invocation + KP_INTERCEPTOR_SEPERATOR + arguments[i];
}
}
window.location = KP_INTERCEPTOR_INVOCATION_COMMAND + KP_INTERCEPTOR_SEPERATOR + invocation;
}
在webView委托中
- (BOOL)webView:(UIWebView *)source shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if([[[request URL] absoluteString] hasPrefix:@"invoke"]) {
[self myFunction];
return NO;
}
return YES;
}
通常,在完成加載后,我會使用Web視圖的scrollView contentSize的高度:
- (void)webViewDidFinishLoad:(UIWebView *)aWebView {
CGFloat newHeight = aWebView.scrollView.contentSize.height;
}
嘗試一下,看看它是否為您返回正確的高度。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.