View Full Version : UIScroll with uilabel, image and webview

Mar 9, 2009, 06:58 AM
Hi, I would like to do something similar to press release detail like New York Times or Usa Today.

I want a scroll with a title, an image and a webview with HTML.

I have done this for now:

scroll=[[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];

CGRect tit =CGRectMake(0.0f, 0.0f, 320.f, 50.0f);
UILabel *title=[[UILabel alloc] initWithFrame:tit];
title.text=[itemNew objectForKey:@"title"];
title.numberOfLines = 2;
title.font=[UIFont fontWithName:@"Helvetica" size:16.0f];
title.textColor=[self getColor:@"00336E"];
[scroll addSubview:title];
[title release];

//Message body
webView = [[UIWebView alloc] initWithFrame:CGRectMake(0.0f, 50.0f, 320.0f, 480.0f)];
[self.webView setScalesPageToFit:NO];
NSString *cuerpo=[itemNew objectForKey:@"body"];
[webView loadHTMLString:cuerpo baseURL:nil];
[scroll addSubview:webView];
[webView release];

[scroll release];

But my title is fixed, only webview does scroll, I would like together (title, image,webview) do scroll in the same layer.

How Could I do it? Thanks

Mar 9, 2009, 10:46 AM
Instantiate a UIScrollView and then add your three elements as subviews of the scrollview.

Mar 9, 2009, 10:59 AM
Yes, I did this before. My scroll didnt have a height and by this didnt work. Now yes, thanks

Mar 9, 2009, 05:47 PM
Now, I need to set the height to the UIScrollView. It must to be equal to my WebView HTML height. How could I calculate the height of my webview?

Mar 9, 2009, 08:20 PM
Now, I need to set the height to the UIScrollView. It must to be equal to my WebView HTML height. How could I calculate the height of my webview?

Mar 10, 2009, 06:27 AM
Yes thanks, but I have problems now sizing webView...

if I put setScalesPageToFit:YES, my HTML looks a lot smaller than I would like. Searching in google, Ive found this trick to optimize web site in Iphone:

Adding in head of my html:
<meta name="viewport" content="width=320"/>

Now it has a font size correct, it works, but my problem is with the height of webView. If its higher than 1200 or 1300px dont display all html ...

I have a method to calculate cgRect frame depending HTML string, UIFont family name, size..., it returns a frame to display my webView, but when height is bigger than 1300 px approximately falls.

webView = [[UIWebView alloc] initWithFrame:calcRect(html,[UIFont boldSystemFontOfSize:16],100,320.0f)];
[webView loadHTMLString:html baseURL:nil];
[scroll setContentSize:CGSizeMake(320.0f,webView.frame.size.height)];
[self.webView setScalesPageToFit:YES];

I would like to ask 2 doubts:
1) Has anyone had problems with webView so high? I have just read that UIView size is 1024px. This is the problem... How could I display the rest of my HTML when height is bigger than 1024px?

2) Is there an other way to calculate webView height according to content?