2010-07-21 96 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]); 
} 
相关问题