【问题标题】:How to resize WebView according to its content?如何根据内容调整 WebView 的大小?
【发布时间】:2010-04-20 13:00:16
【问题描述】:

我想在 Web 视图中设置简单的 html 内容,然后根据其内容调整其大小。

为了在 web 视图中设置简单的 html 内容,我使用了这段代码,它运行良好:

[[myWebView mainFrame] loadHTMLString:webViewContents baseURL:baseURLFramed];

现在,如果内容大于其实际大小,则它会出现在 Web 视图中,其中同时显示垂直和水平滚动条。我想设置一些默认宽度并根据其内容管理高度,以使水平或垂直滚动​​条都不会出现。

谁能给我一些解决方案?

谢谢,

米拉杰

【问题讨论】:

    标签: cocoa webview


    【解决方案1】:

    您可以注册为WebView 的框架加载委托,当页面加载时,您可以询问主框架的文档视图的大小并调整WebView 的大小。确保关闭框架上的滚动条,否则会出现问题。

    请注意,某些页面可能非常大,当我测试 daringfireball.net 时,它的高度为 17612 点,这显然太大而无法在屏幕上显示。

    - (void)applicationDidFinishLaunching:(NSNotification *)aNotification 
    {
        //set ourselves as the frame load delegate so we know when the window loads
        [webView setFrameLoadDelegate:self];
    
        //turn off scrollbars in the frame
        [[[webView mainFrame] frameView] setAllowsScrolling:NO];
    
        //load the page
        NSURLRequest* request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://daringfireball.net"]];
        [[webView mainFrame] loadRequest:request];
    }
    
    //called when the frame finishes loading
    - (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)webFrame
    {
        if([webFrame isEqual:[webView mainFrame]])
        {
            //get the rect for the rendered frame
            NSRect webFrameRect = [[[webFrame frameView] documentView] frame];
            //get the rect of the current webview
            NSRect webViewRect = [webView frame];
    
            //calculate the new frame
            NSRect newWebViewRect = NSMakeRect(webViewRect.origin.x, 
                                               webViewRect.origin.y - (NSHeight(webFrameRect) - NSHeight(webViewRect)), 
                                               NSWidth(webViewRect), 
                                               NSHeight(webFrameRect));
            //set the frame
            [webView setFrame:newWebViewRect];
    
            //NSLog(@"The dimensions of the page are: %@",NSStringFromRect(webFrameRect));
        }
    }
    

    【讨论】:

    • Thanx Rob... 我已经实现了它并且工作正常.. 但有一个问题:它总是为不同内容的 webview 设置最高高度.. 即.如果高度是 1029.0,现在应该是 339.0,那么它会在末尾添加空格并显示高度为 1029.0 的 Web 视图
    • Rob 问题现在已经解决了......在设置它的内容之前,我现在将 Web 视图的大小调整为一些默认值和更小的值......比如说,(15.0、0.0、560.0、10.0)......我必须说 Rob Rocks!
    • 当我在 WebView 中的某些交互过程中更改 DOM 时会触发更改其可见区域的尺寸,此方法 webView:didFinishLoadForFrame 不会再次被调用,对吗?在这种情况下是否会调用一些代表?顺便说一句:您的解决方案对我不起作用。也许你可以帮忙here?
    【解决方案2】:

    另一种方法是向 DOM 询问内容的高度。

    - (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
    {
        NSString*   heightOutput    = [sender stringByEvaluatingJavaScriptFromString:@"document.getElementById(\"foo\").scrollHeight;"];
        NSInteger   height          = [heightOutput integerValue];
    
        [[self splitView] setPosition:height + 10 ofDividerAtIndex:0];
    }
    

    这里的关键点是将您的内容放在命名元素中,以便可以使用 getElementById。我的实现使用了一个简单的<div id="foo"> 元素。

    在我的特殊情况下,我的 WebView 位于 NSSplitView 内,因此首先将 WebView 的高度重置为较小的值会导致闪烁。

    我有一个示例项目here 进行演示。

    【讨论】:

    • 这对我来说比[[[webFrame frameView] documentView] frame] 更好,因为它总是返回正确的值。我不知道为什么,但documentView.frame 仅在第一次加载页面时有效,否则返回空大小(可能是缓存的原因)。
    • 确实,@Darrarski - 这甚至在调整框架大小或调整大小后也有效,即它的宽度发生变化,导致内容高度也发生变化,而接受的答案在这种情况下无济于事,因为它保持报告初始大小。
    猜你喜欢
    • 2016-10-26
    • 1970-01-01
    • 2016-08-04
    • 2010-09-08
    • 2011-01-28
    • 2012-07-28
    • 2014-04-29
    • 2011-09-14
    相关资源
    最近更新 更多