【问题标题】:How to set height of UIWebView based on html content?如何根据 html 内容设置 UIWebView 的高度?
【发布时间】:2012-10-06 13:33:08
【问题描述】:

我想根据 HTML 内容设置 UIWebview 的高度。我正在获取字符串的大小,但由于段落、粗体、不同的字体大小等原因,没有得到实际大小。

【问题讨论】:

  • Webview 有一个滚动条,所以你可能不需要它。
  • 我在 UITablView 的每一行都有 webview。我没有在 heightForRowAtIndexPath 处获得每一行的 webview 高度。所以我很困惑如何在 heightForRowAtIndexPath 中设置每一行的高度。

标签: iphone ios ipad uiwebview


【解决方案1】:

我通常使用这些方法,将UIWebview frame 设置为它的内容大小:

- (void)webViewDidStartLoad:(UIWebView *)webView {
    CGRect frame = webView.frame;
    frame.size.height = 5.0f;
    webView.frame = frame;
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    CGSize mWebViewTextSize = [webView sizeThatFits:CGSizeMake(1.0f, 1.0f)]; // Pass about any size
    CGRect mWebViewFrame = webView.frame;
    mWebViewFrame.size.height = mWebViewTextSize.height;
    webView.frame = mWebViewFrame;

    //Disable bouncing in webview
    for (id subview in webView.subviews) {
        if ([[subview class] isSubclassOfClass: [UIScrollView class]]) {
            [subview setBounces:NO];
        }
    }
}

WebView 完成加载其内容时,它们会被自动调用(如果您将webView 的委托设置为此类)。

【讨论】:

  • 你为什么要遍历子视图来找到滚动视图?只需使用 webView.scrollView。我还认为你应该只在 viewDidLoad 中设置反弹,因为它只需要发生一次。 (这仍然是一个很好的答案。)
  • 感谢您的评论 - 好主意 :) 。我发布的代码真的很旧,从无法直接访问滚动视图的时候开始。
【解决方案2】:

嗯,每个 web 视图都有一个内置的 UIScrollView,所以我会尝试等待页面加载,然后点击滚动视图的 contentSize 属性来获取页面的高度。

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    CGFloat height = webView.scrollView.contentSize.height;
}

【讨论】:

  • 在这段代码中什么是高度。它是网页视图高度的任何属性。
【解决方案3】:

试试这个代码

- (void)webViewDidFinishLoad:(UIWebView *)webView
{     
    height= [[self.webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight"] floatValue];
}

【讨论】:

    【解决方案4】:
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        webview.delegate = self;
        [webview loadHTMLString:@"<div id='foo' style='background: red'>The quick brown fox jumped over the lazy dog.</div>" baseURL:nil];
    }
    
    - (void)webViewDidFinishLoad:(UIWebView *)webView
    {
        NSString *output = [webview stringByEvaluatingJavaScriptFromString:@"document.getElementById(\"foo\").offsetHeight;"];
        NSLog(@"height: %@", output);
    }
    

    另见How to determine UIWebView height based on content, within a variable height UITableView?

    Calculate UIWebView height depending on its content

    【讨论】:

    • 但是我在 UITableView 中有 UIWebview 并且每一行都有 UIWebView 所以我如何计算因为在 cellForRowAtIndex 调用之前设置了表格高度。
    【解决方案5】:

    如果您在 tableviewcell 中有 webview,在 heightForRowAtIndexPath 中将单元格的高度设置为 NSAttributedString 大小的高度,如下所示。

    -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
        NSAttributedString *attributedText = [NSString  getHTMLAttributedString:@"HTML Content"];
        CGSize size = CGSizeMake(300, CGFLOAT_MAX);
        CGRect paragraphRect =     [attributedText boundingRectWithSize:size
                                     options:(NSStringDrawingUsesLineFragmentOrigin)
                                     context:nil];
        return paragraphRect.size.height;
    }
    
    +(NSAttributedString *) getHTMLAttributedString:(NSString *) string{
        NSError *errorFees=nil;
        NSString *sourceFees = [NSString stringWithFormat:
                                @"<span style=\"font-family: 'Roboto-Light';font-size: 14px\">%@</span>",string];
        NSMutableAttributedString* strFees = [[NSMutableAttributedString alloc] initWithData:[sourceFees dataUsingEncoding:NSUTF8StringEncoding]
                                                                                     options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
                                                                                               NSCharacterEncodingDocumentAttribute: [NSNumber numberWithInt:NSUTF8StringEncoding]}
                                                                          documentAttributes:nil error:&errorFees];
        return strFees;
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-16
      • 1970-01-01
      • 2010-10-19
      • 2023-04-10
      • 1970-01-01
      • 2014-10-19
      相关资源
      最近更新 更多