获取UIWebView内容的高度 [英] Get UIWebView content's Height

查看:73
本文介绍了获取UIWebView内容的高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个uiwebview,它使用loadHtmlString函数加载数据.事情是我从sqlite数据库加载数据,每次我加载长度不同的不同字符串时,Web视图自然会获得不同的高度.现在,我需要每次都知道uiwebview的确切高度,以便在webView下方加载imageView. 我尝试过

I have a uiwebview that loads data using the loadHtmlString function.The thing is i am loading the data from an sqlite database, each time i load a different string with different length and naturally the web view obtains different height.
Now i need to know each time the exact height of the uiwebview in order to load an imageView right under the webView. I have tried

- (void)webViewDidFinishLoad:(UIWebView *)aWebView {
    CGRect frame = aWebView.frame;
    frame.size.height = 1;
    aWebView.frame = frame;

    CGSize fittingSize = [aWebView sizeThatFits:CGSizeZero];
    frame.size = fittingSize;
    aWebView.frame = frame;

    NSLog(@"Size: %f, %f", fittingSize.width, fittingSize.height);
}

但是我总是得到相同的结果.我需要知道每次加载时如何动态获取Web视图的高度.谢谢

but i always get the same result. I need to know how can i get dynamically the height of web view each time it is loaded. Thx

推荐答案

这不是处理这种情况的正确方法;而不是将图像加载到图像视图中,而必须始终牢记高度和宽度以及所有这些东西……最简单,最可靠的解决方案就是将图像加载到html字符串中,仅此而已!只需确保baseURL如此,无论您采用哪种方向,一切都可以:

This wasn't the right approach to handle this situation; instead of loading the image in an image view and having to keep always in mind the height and width and all that stuff...The easiest and most reliable solution was just to load the image inside the html string and that's it ! Just be sure of the baseURL as so and everything will be ok no matter what orientation you take :

 NSString* extension = @"gif";
    NSString* strRR = [NSString stringWithFormat:@"%@.%@", authorNAme2,extension];
    NSString *path = [[NSBundle mainBundle] bundlePath];
    //NSLog(@"This is the path %@", path);
    NSURL *baseURL = [NSURL fileURLWithPath:path];
    //  NSLog(@"this is the base URL %@",baseURL);

    NSString *cachePath = [NSString stringWithFormat:@"%@/%@", path,strRR];

            NSString * htmlString = [NSString stringWithFormat:@"\
                                     <html>\
                                     <table width='100%%'>\<tr>\<td>\
                                     <body>\
                                     <p style = 'font-size:30px;'> %@ <\p>\
                                     </td></tr><tr><td><br></td></tr><tr><td align ='center'><br><img src='%@' width='60%%' \></td></tr></body>\
                                     </table>\
                                     </html>",authorNAme , cachePath];



            //          NSString *selection = [WebV2 stringByEvaluatingJavaScriptFromString:@"window.getSelection().toString()"];
            //          NSLog(@"*/*/*/*/**/*/*/*/*/*/*//*/ This is your selection mate %@" , selection);




            WebV.opaque = NO;
            WebV.backgroundColor = [UIColor clearColor];  
            [self.WebV setScalesPageToFit:YES];
            [self.WebV loadHTMLString:htmlString baseURL:baseURL];

这篇关于获取UIWebView内容的高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆