Calculate UIWebView height depending on its content

元气小坏坏 提交于 2019-11-28 20:58:59

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.

Mutix

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 :)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!