UIWebView 在 ScrollView 中加载时的内容更改

编程入门 行业动态 更新时间:2024-10-08 14:49:11
本文介绍了UIWebView 在 ScrollView 中加载时的内容更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我想在用户阅读所选新闻时制作类似 Pulse 新闻的内容.

I want to make something like Pulse news when user read the selected news.

未加载图像时,尚未指定图像的空间.图片加载后,内容的大小也会发生变化(文字会被下推,为图片腾出空间).

When image is not loaded, the space for the image is not yet specified. After the image is loaded, the size of the content also change (the text will be pushed down to make space for the image).

我该怎么做?我现在做的是使用滚动视图,其中包含 uiwebview.

How can i do that? What i do now is using scroll view, with uiwebview inside it.

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

我已经阅读了几个关于此的主题,但我仍然无法解决它.

I've read several topics about this but i still cannot solve it.

- (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.

I load jpg image link on it. The image is loaded but the height is not resize correctly. and i also cannot scroll again the scrollView when the webview is loaded.

下面是我调用要加载的图像时

Below is when i call the image to be loaded

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

谢谢!

推荐答案

我经常在 UIScrollView 中使用 UIWebView,并且因为加载 HTML 是异步完成的,所以你必须在完成后调整 web 视图的大小和滚动视图的内容大小.

I frequently use UIWebView inside UIScrollView, and because loading HTML is done asynchronously you have to resize the web view and the content size of the scroll view when finished.

[webView sizeThatFits:CGSizeZero]

也不适合我.相反,在您的 webViewDidFinishLoad:(UIWebView*)webView 中,您可以通过执行一段 Javascript 来测量 HTML 文档的大小:

does not work for me either. Instead, in your webViewDidFinishLoad:(UIWebView*)webView you can measure the size of the HTML document by executing a piece of Javascript:

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

在此示例中,您必须将 ID 'container' 分配给 HTML 中的顶级元素(确保其周围没有边距).这种方法对我来说非常有效.

In this example, you have to assign the ID 'container' to the top-level element in your HTML (make sure there are no margins around it). This approach works very well for me.

这篇关于UIWebView 在 ScrollView 中加载时的内容更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-29 01:53:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1182558.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:加载   内容   UIWebView   ScrollView

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!