ios webview懒加载怎么得到高度

ios webview懒加载怎么得到高度,第1张

可以 懒加载——也称为延迟加载,即在需要的时候才加载(效率低,占用内存小)。所谓懒加载,写的是其get方法 如果是懒加载的话则一定要注意先判断是否已经有了,如果没有那么再去进行实例化

webVIew在加载html代码完成前,已经把文字和的框架加载好了,但是它总是要把所有html中的都下载完了才回调webViewDidFinishLoad:方法。我现在纠结在这个地方。如何能在加载的时候就获取整体框架的高度

#pragma mark - UIWebview delegete

- (void)webViewDidFinishLoad:(UIWebView )webView

{

CGFloat height = [[webView stringByEvaluatingJavaScriptFromString:@"documentbodyoffsetHeight"] floatValue];

CGRect frame = webViewframe;

webViewframe = CGRectMake(frameoriginx, frameoriginy, framesizewidth, height);

}

WebView只要设置了高度就可以正常滚动,所以我们可以在URL里面加hash的方法让react得到WebView的高度。

我的情况是WebView是HTML代码,如果是URL类似:

WebView内代码:

windowonload = function(){

var height = documentbodyclientHeight;

windowlocationhash = '#' + height;

}

React:

<WebView

javaScriptEnabled={true}

source={{ html: your_html }}

style={{ height: thisstateWebViewHeight }}

onNavigationStateChange={(info)=>{

thissetState({

WebViewHeight:infourlreplace('about:blank%23','')/1

})

}}

/>

页面加载后,webview内的JS代码获取自己的高度,添加一个hash给自己的URL,React当感知到webview加载状态变化后,读取这个hash值,传递给thisstateWebViewHeight,从而改变WebView的高度。

Android开发时,从WebView,我不但想要知道ContentHeight,还想知道ContentWidth。不幸的是,从一个 WebView获取contentWidth是相当困难,因为SDK中没有一个像这样的方法,所以本文为大家呈现了一种实用的解决此问题的方法。

The extensive Android SDK allows you to do many great things with particular views like the WebView for displaying webpages on Android powered devices

Android SDK 的扩展,通过使用特定的view,允许你做许多事情。比如,WebView,用来在Android手机上展示网页。

As of lately while I was experimenting with the Android SDK I was using a WebView in one of my activities

最近,我在体验Android SDK的时候,在一个Activity中用到了WebView。

From that particular WebView I needed to know the ContentHeight but also the ContentWidth

从WebView,我不但想要知道ContentHeight,还想知道ContentWidth。

Now getting the contentHeight is easy like so:

现在的情况是:获取contentHeight很easy,如下:

webviewgetContentHeight();

Unfortunately getting the contentWidth from a WebView is rather more difficult, since there is not a simple method like:

不幸的是,从一个WebView获取contentWidth是相当困难,因为SDK中没有一个像这样的方法:

// THIS METHOD DOES NOT EXIST!

webviewgetContentWidth();

There are ways to get the contentWidth of the rendered HTML page and that is through Javascript If Javascript can get it for you, then you can also have them in your Java code within your Android App

当然是有方法获取contentWidth的,就是通过Javascript来获取。如果你能够支持Javascript,那么你就可以在你的 Android 程序中,使用java代码来获取宽度。

By using a JavascriptInterface with your WebView you can let Javascript communicate with your Android App Java code by invoking methods on a registered object that you can embed using the JavascriptInterface

通过在你的WebView中使用JavascriptInterface,通过调用你注册的JavascriptInterface方法,可以让 Javascript和你的Android程序的java代码相互连通。

So how does this work

怎么做呢?

For a quick example I created a simple Activity displaying a webview that loads a webpage wich displays a log message and a Toast message with the contentWidth wich was determined using Javascript Note that this happens AFTER the page was finished loading, because before the page is finished loading the width might not be fully rendered Also keep in mind that if there is content loaded asynchronously that it doesn't affect widths (most likely only heights will be affected as the width is almost always fully declared in CSS files unless you have a 100% width webpage)

搭建一个快速的例子:创建一个简单的展示webView的Activity,一个LogCat消息,一个Toast消息,用来显示我们通过 Javascript获取的宽度。注意:这些会在网页完全加载之后显示,因为在网页加载完成之前,宽度可能不能够正确的获取到。同时也要注意到,如果是异 步加载,这并不影响宽度(最多高度会受影响,因为宽度总是在CSS文件中做了完全的定义,除非在网页中你用了100%宽度。)。

Below is the code of the Activity Mainjava:

下面的代码是Activity的代码:

01 package compimmosandroidsampleswebviewcontentwidth;

02 import androidappActivity;

03 import androidosBundle;

04 import androidutilLog;

05 import androidwebkitWebView;

06 import androidwebkitWebViewClient;

07 import androidwidgetToast;

08 public class Main extends Activity {

09 private final static String LOG_TAG = "WebViewContentWidth";

10 private final Activity activity = this;

11 private static int webviewContentWidth = 0;

12 private static WebView webview;

13

14 / Called when the activity is first created /

15 @Override

16 public void onCreate(Bundle savedInstanceState) {

17 superonCreate(savedInstanceState);

18 setContentView(Rlayoutmain);

19 webview = (WebView) findViewById(Ridwebview);

20 webviewgetSettings()setJavaScriptEnabled(true);

21 webviewsetSaveEnabled(true);

22 webviewaddJavascriptInterface(new JavaScriptInterface(), "HTMLOUT");

23 webviewsetWebViewClient(new WebViewClient() {

24 @Override

25 public void onPageFinished(WebView view, String url) {

26 webviewloadUrl("javascript:windowHTMLOUTgetContentWidth(documentgetElementsByTagName('html')[0]scrollWidth);");

27 }

28 });

29 webviewloadUrl(">

以上就是关于ios webview懒加载怎么得到高度全部的内容,包括:ios webview懒加载怎么得到高度、UIWebView 加载页面完成前,怎么获取webView内容的高度、ios webview怎么获得内容的高度 简书等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/9332986.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-27
下一篇 2023-04-27

发表评论

登录后才能评论

评论列表(0条)

保存