计算我的UIWebView的高度?

时间:2015-04-06 19:02:09

标签: ios objective-c uiwebview

我正在尝试根据它将显示的内容计算UIWebView所需的高度。所以我出发并编写了以下代码:

_textView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 80, self.view.frame.size.width-20, self.view.frame.size.height+self.view.frame.size.height+self.view.frame.size.height )];
_commentsViewContainer = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetHeight(_backgroundScrollView.frame), CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame) - kBarHeight )];
[_commentsViewContainer addGradientMaskWithStartPoint:CGPointMake(0.5, 0.0) endPoint:CGPointMake(0.5, 0.03)];
_commentsTableView = [[UIScrollView alloc] initWithFrame:CGRectMake(15, 0, self.view.frame.size.width-30, CGRectGetHeight(self.view.frame) - kBarHeight )];
_commentsTableView.scrollEnabled = NO;

...

[_mainScrollView addSubview:_backgroundScrollView];
[_commentsTableView addSubview:_visitWebsite];
[_commentsTableView addSubview:shareButton];
[_commentsTableView addSubview:_textView];
[_commentsViewContainer addSubview:_commentsTableView];
[_mainScrollView addSubview:_commentsViewContainer];

所以在UIWebView完成加载后,我会执行以下操作:

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    NSString *output = [webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight;"];
    NSInteger myInt = [output intValue];

    NSLog(@"Calculated height is: %d", myInt);

    CGRect nextFrame = _textView.frame;
    nextFrame.size.height = myInt + 200;
    _textView.frame = nextFrame;

    CGRect nexterFrame = _commentsTableView.frame;
    nexterFrame.size.height = myInt + 200;
    nexterFrame.origin.y = 0;
    _commentsTableView.frame = nexterFrame;

    CGRect nextererFrame = _commentsViewContainer.frame;
    nextererFrame.size.height = myInt + 200;
    nextererFrame.origin.y = 0;
    _commentsViewContainer.frame = nextererFrame;

    _backgroundScrollView.contentSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height + myInt);
}

然而,它仍然不是正确的大小和文本被切断。我在这里做错了什么?

1 个答案:

答案 0 :(得分:1)

此代码可能会有所帮助。

-(void)webViewDidFinishLoad:(UIWebView *)webView {
     NSLog(@"%f", webView.scrollView.contentSize.height);
}