2010-07-21 101 views
2

我想根據動態文本設置webview的動態高度。我試過根據內容設置webview的動態高度iphone

 [my_webview sizeThatFits:CGSizeZero]; 

但它沒有爲我工作。我想動態設置webview高度。

Thanx提前。

+0

[查看sizeThatFits:]沒有變化查看大小。嘗試[查看sizeToFit],但我不確定即使這將適用於Web視圖,因爲它根據子視圖調整大小,Web視圖沒有任何典型的子視圖。因此,這是一個評論,而不是一個答案。 – lukya 2010-07-21 09:56:26

回答

3

請參閱my answer到類似的問題。訣竅是在發送-sizeThatFits:之前將webView的框架設置爲最小高度。

1

建立自己的Web視圖如下:

- (void)webViewSettings { 
    NSString *htmlString = @"Your HTML String"; 
    NSString* descHtmlString = [NSString stringWithFormat: @"<html><meta name=\"viewport\" content=\"width=300, user-scalable=yes\" /><div style='width:300px' id='ContentDiv'><font face=\"Arial\" size=2.5 color='#702f70'>%@</font></div></html>", htmlString]; 
    descHtmlString = [descHtmlString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; 
    [self.webView loadHTMLString:descHtmlString baseURL:[NSURL URLWithString:@""]]; 
} 

然後實現在您的接口文件UIWebViewDelegate,並在實現文件下面的方法:

- (void)webViewDidFinishLoad:(UIWebView *)webView { 
     NSString *contentHeight = [self.descriptionWeb stringByEvaluatingJavaScriptFromString:@"document.getElementById('ContentDiv').offsetHeight"]; 
     self.descriptionWeb.frame = CGRectMake(10,webView.frame.origin.y, 300, [contentHeight floatValue]); 
} 
相關問題