user4486205
user4486205

Reputation: 105

Calculate height of my UIWebView?

I'm trying to calculate the needed height for a UIWebView based on the content it will display. So I set out and wrote the code below:

_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];

And so after the UIWebView finishes loading, I do the following:

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

However, it's still not the correct size and text gets cut off. What am I doing wrong here?

Upvotes: 0

Views: 141

Answers (1)

Pravin Tate
Pravin Tate

Reputation: 1130

This code might help.

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

Upvotes: 1

Related Questions