2011-11-11 130 views
0

我想在用戶閱讀所選新聞時製作類似Pulse新聞的內容。當UIWebView加載到內部時,內容會發生變化ScrollView

未加載圖像時,圖像的空間尚未指定。加載圖像後,內容的大小也會發生變化(文本將被下推以爲圖像留出空間)。

我該怎麼做? 我現在所做的就是使用滾動視圖,並在其中使用uiwebview。

- (void)loadView 
{ 
self.view = [[UIView alloc] initWithFrame:CGRectZero]; 
UIView *thisView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)]; 
contentTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, 292, 82)];   
contentThumbnailWebView = [[UIWebView alloc] initWithFrame:CGRectMake(10, CGRectGetMaxY(contentTitleLabel.frame), 292, 0)]; 

contentDateLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, CGRectGetMaxY(contentThumbnailWebView.frame), 292, 23)]; 

playVideoButton = [[UIButton alloc] initWithFrame:CGRectMake(CGRectGetMaxX(contentThumbnailWebView.frame)/2, CGRectGetMaxY(contentThumbnailWebView.frame)/2, 72, 37)]; 
[playVideoButton setTitle:@"Play" forState:UIControlStateNormal]; 
playVideoButton.hidden = YES;  

contentSummaryLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, CGRectGetMaxY(contentDateLabel.frame), 292, 20)]; 
contentScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)]; 
contentScrollView.backgroundColor = [UIColor whiteColor]; 

[thisView addSubview:contentScrollView]; 

[contentScrollView addSubview:contentDateLabel]; 
    [contentScrollView addSubview:contentTitleLabel]; 
[contentScrollView addSubview:contentThumbnailWebView]; 
[contentScrollView addSubview:playVideoButton]; 
[contentScrollView addSubview:contentSummaryLabel]; 
//[self.view addSubview:thisView]; 
self.view = thisView; 
contentThumbnailWebView.delegate = self; 
} 

我已經閱讀了幾個關於這個主題,但我仍然無法解決它。

- (void)webViewDidFinishLoad:(UIWebView *)aWebView { 

CGRect frame = aWebView.frame; 
frame.size.height = 1; 
aWebView.frame = frame; 
CGSize fittingSize = [aWebView sizeThatFits:CGSizeZero]; 
fittingSize.width = aWebView.frame.size.width; 
frame.size = fittingSize; 
aWebView.frame = frame; 

NSLog(@"size: %f, %f", fittingSize.width, fittingSize.height); 

[contentDateLabel setFrame:CGRectMake(10, CGRectGetMaxY(aWebView.frame), 292, 23)]; 
playVideoButton = [[UIButton alloc] initWithFrame:CGRectMake(CGRectGetMaxX(aWebView.frame)/2, CGRectGetMaxY(aWebView.frame)/2, 72, 37)]; 
[contentSummaryLabel setFrame:CGRectMake(10, CGRectGetMaxY(contentDateLabel.frame), 292, contentSummaryLabel.frame.size.height)]; 

contentScrollView.contentSize = CGSizeMake(contentScrollView.frame.size.width, CGRectGetMaxX(contentSummaryLabel.frame));    

}

我上加載jpg圖片鏈接。圖像已加載,但高度無法正確調整。而且我也無法在加載webview時再次滾動scrollView。

下面是當我調用加載圖像

- (void) setImageAsThumbnail:(NSString *)imagehumbnailLink 
{    
NSURL *imageURL = [NSURL URLWithString:imageThumbnailLink];  
NSString *cssString = @"<style type='text/css'>img {width: 292px; height: auto;}</style>"; 
NSString *myHTMLContent = @"<html><head></head><body><img src='%@'></body></html>"; 
NSString *htmlString = [NSString stringWithFormat:@"%@%@",cssString,myHTMLContent]; 
NSString *imageHTML = [[NSString alloc] initWithFormat:htmlString, imageURL];  
contentThumbnailWebView.scalesPageToFit = YES; 
[contentThumbnailWebView loadHTMLString:imageHTML baseURL:nil];   
} 

謝謝!

回答

0

我經常在UIScrollView中使用UIWebView,並且因爲加載HTML是異步完成的,所以在完成時必須調整Web視圖和滾動視圖的內容大小。

[webView sizeThatFits:CGSizeZero] 

也不適用於我。相反,在你webViewDidFinishLoad:(UIWebView*)webView你可以通過執行一段JavaScript衡量HTML文檔的大小:

NSString *js = @"document.getElementById('container').offsetHeight;"; 
NSString *heightString = [webView stringByEvaluatingJavaScriptFromString:js]; 
int height = [heightString intValue]; // HTML document height in pixels 

在這個例子中,你必須分配ID「容器」的頂級元素在你的HTML (確保它周圍沒有邊距)。這種方法對我來說效果很好。

相關問題