Reputation: 712
This question follows up on Removing HTML url tags in iOS. I have decided to go with the Web View approach, because it seems like that's the easiest way to get proper links (aka links with actual titles, instead of just the url). But I'm stuck on how to calculate the height of the web view. I use table view cells with web views as subviews.
My current, failing approach:
The table view asks for the height of the cell
- (CGFloat)tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return [CommentTableViewCell heightForCellWithComment:(self.comments)[indexPath.row]];
}
This works well with a text view, because I can just use boundingRectWithSize:options:attributes:context:
to calculate the text height, add some padding on the top and bottom, and return the height. With a web view, I can't do this. I currently take the data returned from the web, which still contains some html tags, insert some html to change the font etc, and then render it in a web view. If I'd use an approach like boundingRectWithSize:options:attributes:context
: the returned height is obviously way off.
This could tries to calculate the height, but that doesn't work and returns 0. The body
property on the commment
contains the html data.
+ (CGFloat)heightForCellWithComment:(Comment *)comment
{
CommentTableViewCell *cell = [[CommentTableViewCell alloc] init];
[cell.textWebView loadHTMLString:comment.body baseURL:nil];
cell.textWebView sizeThatFits:
CGSizeMake(cell.textWebView.frame.size.width, CGFLOAT_MAX)];
return cell.textWebView.frame.size.height + 48.0f; // 48 px padding
}
UITableViewDataSource asks for cell. I dequeue a cell from the storyboard, and set a comment
property on it.
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CommentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:...];
Comment *comment = ...;
cell.comment = comment;
return cell;
}
So what I'd say I'm looking for is a way to calculate the web view's dimensions before all the table view stuff kicks in. Thank you in advance.
Upvotes: 1
Views: 1049
Reputation: 6198
Note the webViewDidFinishLoad:
is not the real time of the webView finish loading.You'd better use :
- (void)webViewDidFinishLoad:(UIWebView *)webView{
// the real time of webView finish loading
if ([webView isFinishLoading] == YES) {
// do something you want
}
}
Upvotes: 0
Reputation: 1161
Loading the HTML string is done asynchronously. You have to set a delegate for the UIWebView
and wait for the webViewDidFinishLoad:
call. In this call you can query the web view for its height by accessing the webView.scrollView.contentSize.height
. Then you can store the height and tell the table view to reload the particular cell, it will then have the correct height.
Upvotes: 2
Reputation: 57188
Is cell.textWebView
nil in your heightForCellWithComment:
method? If you're using a storyboard for your cell, you'll need to dequeue a cell from your table view for this purpose so that its views are actually loaded. (Just calling alloc/init will not create the subviews, like the webview, from the storyboard.)
If you don't want to have to dequeue it from the table view, you might move your view content out to a separate nib which you can load directly. (For performance reasons, you should probably load/dequeue it once and re-use it for each call to heightForCellWithContent:
.)
Upvotes: 1