ios 如何根据html内容设置UIWebView的高度?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12914898/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 20:46:57  来源:igfitidea点击:

How to set height of UIWebView based on html content?

iphoneiosipaduiwebview

提问by KPIteng

I want to set height of UIWebview based on HTML content. I am getting the size of a string, but not getting the actual size due to paragraph, bold, different font size, etc.

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

采纳答案by Guntis Treulands

I usually use these methods, to set UIWebviewframe as it's content size:

我通常使用这些方法来设置UIWebview框架的内容大小:

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

They are automatically called (if you set webView's delegate to this class), when WebViewhas finished loading it's content.

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

回答by Mick MacCallum

Well, every web view has a UIScrollView built into it, so I would try waiting for the page to load and then tapping into the scroll view's contentSize property to get the height of the page.

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

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

回答by Mick MacCallum

Try this code

试试这个代码

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

回答by Venk

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

see also How to determine UIWebView height based on content, within a variable height UITableView?

另请参阅如何在可变高度 UITableView 内根据内容确定 UIWebView 高度?

Calculate UIWebView height depending on its content

根据其内容计算 UIWebView 高度

回答by Rajan Twanabashu

If you have webview in tableviewcell, in heightForRowAtIndexPath set the height of cell to height of NSAttributedString size as following.

如果您在 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;

}