How to Determine Uiwebview Height Based on Content, Within a Variable Height Uitableview

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

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath: (NSIndexPath *) indexPath { 
UITableViewCell *cell = [self tableView: tableView cellForRowAtIndexPath: indexPath];
return cell.bounds.size.height;
}

UITableViewCell with UIWebview that adjusts height to content size

The auto layout approach might be tricky. An easier approach is to just set the frame of the corresponding cell in webViewDidFinishLoad. You can use cellForRowAtIndexPath on the table view to get the displayed cell (it won't try to get the cell from the UITableViewDataSource if it's already displayed because it'll be cached).

- (void)webViewDidFinishLoad:(UIWebView *)webView {
if (_contentHeights[webView.tag]) {
return;
}
float height = [NSNumber numberWithFloat:[[webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight;"] floatValue]];

NSIndexPath* indexOfCell = [NSIndexPath indexPathForRow:webView.tag inSection:0];
UITableViewCell* cell = [self.tableView cellForRowAtIndexPath:indexOfCell];
cell.frame = CGRectMake(0, 0, cell.frame.size.width, height);
}

Calculate UIWebView height depending on its content

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.

UITableViewCell With UIWebView Dynamic Height

TableView will resize cells itself, you just need implement tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat delegate method.

Yes, you don't know the height of WebView initially, but you can calculate it and then ask TableView to reload cell. Something like this:

class TableViewController: UITableViewController, UIWebViewDelegate
{
var content : [String] = ["test1
test1
test1
test1
test1
test1", "test22
test22
test22
test22
test22
test22"]
var contentHeights : [CGFloat] = [0.0, 0.0]

// ...

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCellWithIdentifier("newsCell", forIndexPath: indexPath) as! NewsTableViewCell
let htmlString = content[indexPath.row]
let htmlHeight = contentHeights[indexPath.row]

cell.webView.tag = indexPath.row
cell.webView.delegate = self
cell.webView.loadHTMLString(htmlString, baseURL: nil)
cell.webView.frame = CGRectMake(0, 0, cell.frame.size.width, htmlHeight)

return cell
}

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
{
return contentHeights[indexPath.row]
}

func webViewDidFinishLoad(webView: UIWebView)
{
if (contentHeights[webView.tag] != 0.0)
{
// we already know height, no need to reload cell
return
}

contentHeights[webView.tag] = webView.scrollView.contentSize.height
tableView.reloadRowsAtIndexPaths([NSIndexPath(forRow: webView.tag, inSection: 0)], withRowAnimation: .Automatic)
}

// ...
}

Determine height of UIWebView content (embedded in UITableViewCell)

it works but only when

  • making webview small (like 5px)
  • load it and wait for finish
  • calling sizeToFit on it then
  • getting the size THEN

advertisement: :D see my M42WebviewTableViewCell class on github which struggles with this

Change UITableViewCell height based on webview content

Found a solution ...

  • In webViewDidFinishLoad(_ webView: UIWebView) set the constraint's
    height as in initial question but use document.body.clientHeight
  • Blast a notification to tell the associated tableview to not reloadData(), but rather beginUpdates() then endUpdates()

@objc func handleWebviewFinishedLoading() {
self.webview?.beginUpdates()
self.webview?.endUpdates()
}

This seems to redraw the cells without reloading them :)

Calculate UIWebView height depending on its content

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.

Dynamic height for uiwebview with html data

I got the trick,

just replaced the

  • with
    before setting it to attributed string

       valueString = [valueString stringByReplacingOccurrencesOfString:@"
  • " withString:@"
    "];


  • Related Topics



  • Leave a reply



    Submit