0

I have a class named Announcement, it has a Title (NSString), a Publishing Date (NSString) and a Body (NSString with HTML content).

I would like to display it in a similar way like in the Email app. When you read an email you have the first three "rows" (From, To, Title[date]) and then the content of the email.

I would want to create only the "Title[date]" row and then the body. I imagine that it to be a UITableView which has on the first cell my Title and on the second cell an UIWebView.

But it seems like the cell has exactly the height of the UIWebView, so my question is this:

How can calculate the height of my UIWebView depending on my Body? Because basically when you scroll vertically you scroll the whole UITableView, and when you scroll horizontally you scroll only the UIWebView content.

Thanks

Mutix
  • 4,196
  • 1
  • 27
  • 39
Cosmin
  • 2,840
  • 5
  • 32
  • 50
  • possible duplicate of [How to determine the content size of a UIWebView?](http://stackoverflow.com/questions/3936041/how-to-determine-the-content-size-of-a-uiwebview) – Ortwin Gentz Aug 08 '13 at 10:16

2 Answers2

4

In webview delegate method, just try with below code:

- (void)webViewDidFinishLoad:(UIWebView *)webView{
    [webviewTopicDesc sizeToFit];
    [webviewTopicDesc setFrame:CGRectMake(webviewTopicDesc.frame.origin.x, webviewTopicDesc.frame.origin.y, 300.0, webviewTopicDesc.frame.size.height)];
}

Before that, make the webview height as 5.0.

Hope it helps to you.

Here, you can get dynamic height based on string.

-(CGSize)getDynemicHeight:(int)pintFixWidth :(NSString *)pstrText
{
CGSize maximumSize=CGSizeMake(pintFixWidth, g_Max_Width);
UIFont *myFont = [UIFont fontWithName:g_Default_Font_Name size:g_Default_Font_Size];// font used for label
myFont=[UIFont boldSystemFontOfSize:g_Default_Font_Size];

CGSize sizeS = [pstrText sizeWithFont:myFont 
                   constrainedToSize:maximumSize 
                       lineBreakMode:UILineBreakModeWordWrap];

sizeS.height=sizeS.height+39;

return sizeS;
}

Here, you have to fix the width and font-size with font-style.

May be it helps to you.

Nishant B
  • 2,897
  • 1
  • 18
  • 25
  • It works, but I want the minimum content size (or zoom scale). Basically, when I load the web view I want to display the full content. – Cosmin May 23 '12 at 08:38
  • I have update my answer and add function to get the dynamic height based on string length. Hope it may helps to you. – Nishant B May 23 '12 at 10:17
0

There are a few ways of finding the height of a UIWebView's content.

Here are a few pointers to start with:

How to determine UIWebView height based on content, within a variable height UITableView?

How to determine the content size of a UIWebView?

iPhone - get the Content Size of UIWebView

Note that using a UIWebView in a UITableView isn't necessarily a very good idea, specially if you have many Announcements to scroll through in the same UITableView. If you show only one Announcement per UITableView, you should be able to get away with it, but for tables with a lot of cells to scroll through, using webviews in the cells can lead to bad performance issues.

Apple also recommend against using embedding UIWebViews / UITableViews / UIScrollViews.

From the UIWebView Class Reference :

Important: You should not embed UIWebView or UITableView objects in UIScrollView objects. If you do so, unexpected behavior can result because touch events for the two objects can be mixed up and wrongly handled.

My 2 cents :)

Community
  • 1
  • 1
Mutix
  • 4,196
  • 1
  • 27
  • 39
  • I want the minimum content size (or zoom scale). Basically, when I load the web view I want to display the full content. – Cosmin May 23 '12 at 08:39
  • In that case you would need to set the `UIWebView`'s `scalesPageToFit` property to `TRUE` – Mutix May 23 '12 at 09:48
  • I want my UIWebView to have the following frame=CGRectMake(0,0,320,height),and I want the content to be have the exactly same "width" (320) and to adjust it's height accordingly. For instance,if I disable scalesPageToFit,and I create a frame for my UIWebView of (0,0,320,480), the content fix horizontally and I can only scroll vertically. I want something similar, only that I want the height to be already set according to the content's height. And also, I want to be able too zoom, but if I set scalesPageToFit then initially the content is not fit into my width.Sorry for not making myself clear. – Cosmin May 23 '12 at 09:58
  • 1
    As long as you make the `UIWebView` inside the table without "user interaction enabled" it's fine to embed them. As stated in your excerpt the issue is from handling touch events. Besides.... there is no other simple way to render html. – DBD Aug 28 '12 at 13:03