-2

NSString含む がありますHTML tags。したがって、正しく表示するために、UIWebview. ここで、コンテンツに合わせて UIWebview のフレームのサイズを変更したいと思います。これを に追加しUIWebviewていtableHeaderます。今、これが私がしていることです。

私の見解では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 の下に別のラベルを追加します。問題は、viewDidLoad. だから内容に合わない。

誰でもこれで私を助けることができますか?

4

1 に答える 1

2

次の型を使用して、コンテンツの高さを取得できますUIWebView

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

それがあなたを助けることを願っています..

于 2013-05-23T08:38:18.907 に答える