Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
438 views
in Technique[技术] by (71.8m points)

iphone - Calculate UIWebView height depending on its content

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...