2015-04-06 50 views
0

我試圖根據它將顯示的內容計算所需的高度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); 
} 

但是,它仍然不是正確的大小和文字被切斷。我在這裏做錯了什麼?

+0

您是否試圖用這行代碼獲取字符串的大小? NSInteger myInt = [output intValue]; –

+0

不,我從'NSString * output = [webView stringByEvaluatingJavaScriptFromString:@「document.body.scrollHeight;」];''我得到了一個像'1685'這樣的字符串,我將它轉換爲一個整數。 – user4486205

回答

1

此代碼可能會有所幫助。

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