2010-04-20 124 views
11

我想在web視圖中設置簡單的html內容,然後根據其內容調整它的大小。如何根據其內容調整WebView的大小?

要設置我用這個代碼的Web視圖中簡單的HTML內容,並將其工作正常:眼下

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

,如果含量大於它的實際大小,然後它出現在Web視圖顯示垂直和水平卷軸在它。我想設置一些默認寬度和管理高度根據其內容的方式,使橫向和縱向滾動不出現。

任何人都可以爲我提供一些解決方案嗎?

感謝,

Miraaj

回答

20

可以作爲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)); 
    } 
} 
+0

Thanx Rob ...我已經實現了它,它工作正常..但有一個問題:它總是設置最高的webview,我得到不同的內容的高度..即。如果高度是1029.0,現在它應該是339.0,那麼它是在末尾追加空格,並顯示高度的web視圖1029.0 – Miraaj 2010-04-21 07:16:10

+3

Rob現在解決了問題。在設置其內容之前,我現在將web視圖的大小調整爲某些默認值和較小的值。 ..說,(15.0,0.0,560.0,10.0)..我必須說羅布巖! – Miraaj 2010-04-21 10:29:04

+0

當我在WebView中的某些交互過程中更改DOM時,會觸發更改其可見區域的尺寸,此方法「webView:didFinishLoadForFrame」將不會再次調用,是true?在這種情況下是否會有一些代表被調用?順便說一句:你的解決方案不適合我。也許你可以幫忙[這裏](http://stackoverflow.com/q/21992854/1146700)? – beipawel 2014-02-24 16:20:45

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演示它。

+1

這對我來說比'[[[webFrame frameView] documentView] frame]'更好,因爲它總是返回適當的值。我不知道爲什麼,但'documentView.frame'只有在第一次加載頁面時才起作用,否則它會返回空白大小(可能是因爲緩存)。 – Darrarski 2013-12-03 14:23:54

+0

事實上,@Darrarski - 這甚至可以在調整gor大小後重新調整幀的大小,即寬度發生了變化,導致內容高度發生變化,而接受的答案在這種情況下無法幫助,因爲它會不斷報告初始大小。 – 2018-01-09 08:40:07

相關問題