【问题标题】:Get UIWebView content's Height获取 UIWebView 内容的高度
【发布时间】:2012-10-18 20:22:34
【问题描述】:

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

- (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 视图的高度。谢谢

【问题讨论】:

    标签: ios uiwebview height xcode4.3


    【解决方案1】:

    试试这个

    NSString *result = [webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight;"];
    
    int height = [result integerValue];
    

    在您的 webViewDidFinishLoad 方法中。

    【讨论】:

    • 打算建议这个。 +1。
    • 我将如何在变量中得到结果?我没有看到您的建议的结果,我该如何处理?我是新人,谢谢理解
    • @EliasRahme 您将结果转换为返回的字符串:NSString *result = [webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight;"]; 要将 result 字符串转换为 int,您可以使用:int height = [result integerValue];
    • [webView stringByEvaluatingJavaScriptFromString:@"Math.max( document.body.scrollHeight, document.body.offsetHeight, document.documentElement.clientHeight, document.documentElement.scrollHeight, document.documentElement.offsetHeight );" ]
    • @iMx 你的评论工作得很好,你能解释一下为什么我们需要获得所有这些价值的最大值吗?
    【解决方案2】:

    这不是处理这种情况的正确方法;而不是在图像视图中加载图像并且必须始终牢记高度和宽度以及所有这些东西......最简单和最可靠的解决方案就是将图像加载到 html 字符串中,就是这样!只要确保 baseURL 如此,无论您采取什么方向,一切都会好起来的:

     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];
    

    【讨论】:

      【解决方案3】:

      您可以通过访问网络视图的scrollView 属性来获取网络视图中内容的高度:

      NSLog(@"%f",myWebView.scrollView.contentSize.height);
      

      【讨论】:

      • 优于 JS insection。也为你 +1。
      • 我试过了,但同样的数字还在继续……即使我有一个 2 个单词的段落,我得到 737 所以它不起作用:S
      • @EliasRahme 你能否更好地解释一下你在这里想要达到的目标。我在上面列出的代码您如何跟踪 web 视图中内容的大小。
      • 当网页视图加载一段文字时......它会自动增加高度,因为我正在使用 [self.WebV setScalesPageToFit:YES];所以我每次加载一段单词时都需要..我得到它的确切新高度,以便在她的正下方放置一个图像视图
      • 这对我也不起作用,而 JS 注入起作用了。我也在加载长文本和多个段落。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多