-2

我有一个NSString包含HTML tags. 因此,为了正确显示它,我使用的是UIWebview. 现在我想调整 UIWebview 的框架以适应内容。我将此添加UIWebviewtableHeader. 现在这就是我所做的。

在我看来DidLoad

UIWebView *webView = [[UIWebView alloc]initWithFrame:CGRectMake(10, height2,self.view.bounds.size.width-30, 20)];
    [[webView scrollView] setBounces: NO];
    webView.delegate = self;
    [self.headerView addSubview:webView];

    [self setWebDescription:webView];

然后在 setWebDescription

-(void)setWebDescription:(UIWebView *)webDescription
{
    NSString *myDescriptionHTML = [NSString stringWithFormat:@"<html> \n"
                                   "<head> \n"
                                   "<style type=\"text/css\"> \n"
                                   "body {font-family: \"%@\"; font-size:15; font-color:\"%@\";}\n"
                                   "</style> \n"
                                   "</head> \n"
                                   "<body>%@</body> \n"
                                   "</html>",@"TeutonNormal",@"#AAAAAA", _newsItem.new_body];
    [webDescription setBackgroundColor:[UIColor clearColor]];
    [webDescription setOpaque:NO];
    [webDescription loadHTMLString:myDescriptionHTML baseURL:nil]; 
}

最后在我的 webViewDidFinishLoad

- (void)webViewDidFinishLoad:(UIWebView *)aWebView {
    CGRect newBounds = aWebView.bounds;
    [aWebView sizeToFit];
    newBounds.size.height = aWebView.scrollView.contentSize.height;
    aWebView.bounds = newBounds;
    aWebView.scrollView.bounds = newBounds;
    aWebView.contentMode = UIViewContentModeRedraw;

    NSLog(@"Height is %f",newBounds.size.height);

    newHeight =  height2 + aWebView.frame.size.height;


    UILabel *lblLikes = [[UILabel alloc]initWithFrame:CGRectMake(40, newHeight+10, 160, 40)];
    lblLikes.text = _newsItem.new_publishdate;

    float newHeight2 =  newHeight + 80;
    NSLog(@"new height header is %f",newHeight2);
    [self.headerView setFrame:CGRectMake(0,0,self.view.bounds.size.width,newHeight2)];
        [headerView addSubview:lblLikes];

    [self.tableView setBounces:NO];
    self.tableView.tableHeaderView = headerView;

我还在 webview 下方添加了另一个标签。现在的问题是我的 webview 总是 20px 高,就像我在viewDidLoad. 所以不符合内容。

有人可以帮我吗?

4

1 回答 1

2

您可以使用以下类型来获取 的内容高度UIWebView

CGFloat newHeight = [[self.answerWebView stringByEvaluatingJavaScriptFromString:@"document.documentElement.scrollHeight;"] floatValue];

希望对你有帮助。。

于 2013-05-23T08:38:18.907 回答