Determine height of UIWebView content (embedded in UITableViewCell) - uitableview

I've got a UITableView holding UIWebViews of variable height, and the user has the ability to edit the content of each web view. The content of each cell (a web view) is linked to a NSManagedObject subclass that has the height of the content stored as contentHeight. When a new set of content is initially created, it is filled in with a default set of data that has a set height, so there is no issue there.
My problem arises when the user edits the content of a web view; I need to figure out the height of the new content and update the tableview accordingly. I've already set up the logic for updating the saved contentHeight variable for the corresponding object, but I cannot find a reliable method to determine the height of the new content. Below I've listed a few of the methods I've tried and haven't been able to make work. ANY help would be greatly appreciated, I've been stumped for over an hour now :(
FAILED METHODS:
[[webView stringByEvaluatingJavaScriptFromString:#"document.documentElement.scrollHeight"] floatValue];
[[webView stringByEvaluatingJavaScriptFromString:#"document.body.scrollHeight"] floatValue];
[[webView stringByEvaluatingJavaScriptFromString:#"document.body.offsetHeight"] floatValue];
[[webView stringByEvaluatingJavaScriptFromString:#"document.getElementById('sdcontent').offsetHeight"] floatValue];
CGRect webViewFrame = [webView frame];
CGRect temp = webViewFrame;
webViewFrame.size.height = 1;
[webView setFrame:webViewFrame];
CGSize fittingSize = [webView sizeThatFits:CGSizeZero];
webViewFrame.size = fittingSize;
[webView setFrame:temp];

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

Related

How to calculate web view content height in iOS?

I need to resize the web view frame according to content height. So I need to calculate the height of content.
This is the solution that i use
NSUInteger contentHeight = [[webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:#"document.body.scrollHeight;"]] intValue];
Your webview should have a frame smaller than all possible content size
Try This,
- (void)webViewDidFinishLoad:(UIWebView *)webView {
CGSize contentSize = webView.scrollView.contentSize;
NSLog(#"Content Size %#", NSStringFromCGSize(contentSize));
}

Get height of UIWebView loaded content

So what I am after is a way to put a UIButton on top of a UIScrollView and a UIWebView under it. The reason I want it this way is that I need the button to "scroll away" as the user scrolls the page I load into the UIWebView.
In order for this to work, I need to get the height of the content of the web page I load and then set the height of the web view to match this. If I can do this, I then intend to set the contentSize of the UIScrollView to match the heights of the button and web view.
I understand that somehow this is supposed to happen in the - (void)webViewDidStartLoad:(UIWebView *)webView method, which is confirmed on many threads on stackoverflow (for example this one). I also know that Apple says you shouldn't put a UiWebView in a UIScrollView, since the scrolling actions may interfere. Disabling scroll for the web view should avoid the problem though.
There are many threads discussing this matter, but none of them seem to work for me. Can it be because I am running on iOS7?
I am in big desperation here, help is much appreciated!
You can get height using following code :
NSString *heightStrig = [webView stringByEvaluatingJavaScriptFromString:#"(document.height !== undefined) ? document.height : document.body.offsetHeight;"];
float height = heightStrig.floatValue;
complete code :
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
CGRect frame = webView.frame;
NSString *heightStrig = [webView stringByEvaluatingJavaScriptFromString:#"(document.height !== undefined) ? document.height : document.body.offsetHeight;"];
float height = heightStrig.floatValue + 10.0;
frame.size.height = height;
webView.frame = frame;
}
You can get the height of the content loaded into a UIWebView as follows:
- (void)webViewDidFinishLoad:(UIWebView *)webView {
CGFloat contentHeight = webView.scrollView.contentSize.height;
}
And then adjust the frame of the web view accordingly.

How to resize the width of the UITextView according the?

I want to ask how to resize the UITextView to fit the text will display on the screen.
For example
Resize the UITextView to
Could anyone help me out or give me some advise.
Thanks
Tips:
I tried
sizeWithFont:constrainedToSize: ,Not work
If you need to know the CGSize that is taken by your NSString if rendered on a single line, use CGSize sz = [textView.text sizeWithFont:_textView.font] then adjust your frame given this size value.
If you need to know the size taken by your text if rendered on multiple lines, wrapping it if the text reaches a given width, use sizeWithFont:constrainedToSize:lineBreakMode: instead, etc.
I had the same problem some time ago (but with the height of the UITextView) and I created a CGRect with a size that would fit the contentSize of the UITextView, Try the code below
// assuming that you have a UITextView named textView with some text on it
CGRect textFrame = self.textView.frame;
textFrame.size.width = self.textView.contentSize.width;
self.textView.frame = textFrame;
try this one it worked for me
CGRect frame = _textView.frame;
frame.size.height = _textView.contentSize.height;
frame.size.width = _textView.contentSize.width;
_textView.frame = frame;
happy coding....

ios create 'readmore'. Let superview be dependent of subview

gaaah designing in ios gives me headache!!
So please, help me out and maby explain to me how one should think trying to come up with the solution.
I have:
As you can see the UITextField has its frame being set in storyboard to be smaller than its actual content.
Everything above the prototypecell is within a UIView that is set to be tableHeader programatically.
I want to:
Press the read more btn so that the UITextField get its actual size. No problem, I can do that with getting the contentSize programmatically. It works but it ofc overflows the tableHeader
So I thought, good. Then all I have to do is set the tableHeader to be the size of the 'new' calculated height of UITextField + height of the 2 UIImageViews.
But nope. it only resizes to the existing height set in storyboard insted. In other word, it does one or the other.
And using autolayout it totally breaks but not giving me any errors about constraints.
This seems so easy wich makes me feel so stupid haha
this is what I have i code
- (IBAction)toggleReadMore:(id)sender{
_toggleReadMoreBtn.hidden = YES;
CGRect textFrame = _cityDescription.frame;
_cityDescription.frame = textFrame;
CGRect tableHeaderViewFrame = CGRectMake(0, 0, self.tableView.tableHeaderView.frame.size.width, _cityDescription.contentSize.height + 218.0f ); //textFrame.size.height
self.tableView.tableHeaderView.frame = tableHeaderViewFrame;
textFrame.size.height = _cityDescription.contentSize.height;
[self.tableView setTableHeaderView:self.viewForTableHeader];
please, guide me how to think
- (IBAction)readMoreBtnClicked:(UIButton *)sender
{
NSLog(#"Read more Btn Clicked");
NSString *stringToBeDisplayed = #"Any Text Here";
CGSize textSize=[stringToBeDisplayed sizeWithFont:[UIFont systemFontOfSize:30]
constrainedToSize:CGSizeMake(270, 500)
lineBreakMode:NSLineBreakByWordWrapping];
NSLog(#"textSize = %#",NSStringFromCGSize(textSize));
[self.textView setFrame:CGRectMake(self.textView.frame.origin.x,self.textView.frame.origin.y, textSize.width, textSize.height)];
NSLog(#"self.textView.frame = %#",NSStringFromCGRect(self.textView.frame));
[self.textView setText:stringToBeDisplayed];
[self.headerView setFrame:CGRectMake(self.headerView.frame.origin.x,self.headerView.frame.origin.y, 320, dynamicHeightCalculatedAfterTextSize)];
[self.tableView reloadData];
}

Is there any similar solution to UIWebView?

I use a UIWebView in my app to present content, but now I can't do it anymore because I also need to use a UIScrollView and it creates conflicts related to scrolling.
Also, UIWebView is very slow.
So my question is : is there any another way to load string which contains HTML tags (strong, p, div, and etc...)?
UPDATE (improved explanation)
I have UIVIewController containing an image at the top, and under this image is a title and under the title is content (HTML string from web). The problem is that when the text is too big, the webview is scrolling, but not the whole page. I want the whole page to scroll, not just the webview.
You won't get anything faster or better than the native UIWebView. Probably you should overthink your UI/UX. What exactly do you want to achieve with a webview in a scrollview??
Perhaps attributed strings are enough for you. Look it up.
The OHAttributedLabel even parses HTML for you.
Solved.
-(void)webViewDidFinishLoad:(UIWebView *)webView {
NSString *output = [webView stringByEvaluatingJavaScriptFromString:#"document.body.scrollHeight;"];
int webViewHeight = [output intValue];
//NSLog(#"height: %d", webViewHeight);
if(webViewHeight > 220){
CGRect frame = articleContentWebView.frame;
frame.size.height = webViewHeight;
articleContentWebView.frame = frame;
int scrollViewheight = 416 + (webViewHeight - 220);
[articleScrollView setContentSize:CGSizeMake(320, scrollViewheight)];
}
}
In delegate method webViewDidFinishLoad I calculate height od UIWebView content and then set height of UIWebVIew and height of UIScrollView.

Resources