【问题标题】:Set dynamic height of webview depending on the content on an iPhone根据 iPhone 上的内容设置 webview 的动态高度
【发布时间】:2010-07-21 09:18:53
【问题描述】:

我想根据动态文本为 webview 设置动态高度。我试过了

       [my_webview sizeThatFits:CGSizeZero];

但这对我不起作用。我想动态设置 webview 的高度。

【问题讨论】:

  • [view sizeThatFits:] 不会改变视图大小。尝试 [view sizeToFit] 但我不确定这是否适用于 Web 视图,因为它会根据子视图调整大小,并且 Web 视图没有任何典型的子视图。因此,这是评论而不是答案。

标签: ios iphone cocoa-touch uiwebview uikit


【解决方案1】:

请参阅my answer 了解类似问题。诀窍是在发送 -sizeThatFits: 之前将 webView 的框架设置为最小高度。

【讨论】:

    【解决方案2】:

    如下设置您的网络视图:

    - (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]);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-04
      • 1970-01-01
      • 1970-01-01
      • 2019-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-07
      相关资源
      最近更新 更多