I have taken a web view and load html like this
NSMutableString *myDescriptionHTML = [NSMutableString stringWithFormat:#"<html> \n"
"<head> \n"
"<style type=\"text/css\"> \n"
"body {font-family: \"%#\"; font-size: %#;}\n"
"</style> \n"
"</head> \n"
"<body>%#</body> \n"
"</html>", #"helvetica", [NSNumber numberWithInt:40], messageModel.content];
i want to set web view height to web view content size height after web view loaded html content
Tried ways
1)
CGRect frame = self.webViewMain.frame;
frame.size.height = 1;
self.webViewMain.frame = frame;
CGSize fittingSize = [self.webViewMain sizeThatFits:CGSizeZero];
frame.size = fittingSize;
self.webViewMain.frame = frame;
Height = fittingSize.height;
NSLog(#"size: %f, %f", fittingSize.width, fittingSize.height);
[tableViewMain reloadData];
its too long height
2)
Height = [[self.webViewMain stringByEvaluatingJavaScriptFromString:#"document.documentElement.scrollHeight;"] floatValue];
3)
Heiht = self.webViewMain.scrollView.contentSize.Height
But no one is actual height
help me thanks....
UIWebview is a subclass of UIScrollview so you can set Webview.content size using CGSize. That will set your web view content height and width as required.
Related
I am just developing and application just like news feeding. Some of the cell view I have to set NSAttributedString to textview and get the exact height of textview.
In my NSAttributedString there is HTML content. I have to set in textview because its takes too much time in web view.
The problem is that some of the time I get the perfect height of of textview and some of the time I'm not getting the height of textview. Because ofNSAttributedString some time it considers font height and some time it is not considering it a font height.
You can get more idea if you see my code about what I have done. templbl2 is a UITextview temptext2 is UIView.
Here is my code:
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *str3 = [NSString stringWithFormat:#"<span style=\"font-family: Frank-regular; font-size: 13\">%#</span>", strTerms];
NSAttributedString * attrStr2 = [[NSAttributedString alloc] initWithData:[str3 dataUsingEncoding:NSUTF8StringEncoding] options:#{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSFontAttributeName : [UIFont fontWithName:#"Frank-regular" size:13.0]} documentAttributes:nil error:nil];
templbl2.attributedText = attrStr2;
[templbl2 sizeToFit];
[self textViewHeightForAttributedText:attrStr2];
}
- (CGFloat)textViewHeightForAttributedText:(NSAttributedString *)text
{
CGFloat width = [UIScreen mainScreen].bounds.size.width; // whatever your desired width is
CGRect paragraphRect =
[text boundingRectWithSize:CGSizeMake(300.f, CGFLOAT_MAX)
options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)
context:nil];
temptext2.frame = CGRectMake(0, 0, width, paragraphRect.size.height+60);
return paragraphRect.size.height;
}
Try below method:
- (CGFloat)textViewHeightForAttributedText:(NSAttributedString *)text
{
CGFloat width = [UIScreen mainScreen].bounds.size.width; // whatever your desired width is
UITextView *txtView;
txtView.attributedText = attrStr2;
CGSize size = [tvDummyForHeightTemp sizeThatFits:CGSizeMake(width, FLT_MAX)];
temptext2.frame = CGRectMake(0, 0, width, size.height+60);
return size.height;
}
Hope this will help:)
I'm trying to use UIWebView inside UIScrollView, I want to display an webview under an image and make them scrollable.
So I flowed those step :
Desactivate the Scroll Property for UIWebView
Set the UIScrollView and UIWebView height equals to content size.
And this my code :
- (void)viewDidLoad {
[super viewDidLoad];
self.contentReader.scrollView.scrollEnabled = NO;
NSString *myDescriptionHTML = [NSString stringWithFormat:#"<html> \n"
"<head> \n"
"<style type=\"text/css\"> \n"
"body {font-family: \"%#\"; font-size: %d;}\n"
"</style> \n"
"</head> \n"
"<body>%#</body> \n"
"</html>", #"JF Flat", 18, self.thePost.content];
[self.contentReader loadHTMLString:[NSString stringWithFormat:#"<div style='text-align:right; text-align:justify; direction:rtl'><style type='text/css'>img { max-width: 100%%; width: auto; height: auto; }</style><style type='text/css'>iframe { max-width: 100%%; width: auto; height: auto; }</style>%#<div>",myDescriptionHTML] baseURL:nil];
}
-(void)webViewDidFinishLoad:(UIWebView *)webView {
// get the html content height
NSString *output = [webView stringByEvaluatingJavaScriptFromString:#"document.height;"];
NSLog(#"Content Size %#",output);
// adjust the height of webView
NSLog(#"WebView Hieght before Update%f", webView.frame.size.height);
CGRect frame = webView.frame;
frame.size.height = 1;
webView.frame = frame;
CGSize fittingSize = [webView sizeThatFits:CGSizeZero];
frame.size = fittingSize;
webView.frame = frame;
NSLog(#"WebView Hieght After Update%f", webView.frame.size.height);
[self.scroller setContentSize:webView.bounds.size];
}
And This is the Log message :
[5150:203791] WebView Hieght before Update360.000000
[5150:203791] WebView Hieght After Update5888.000000
The Problem is that when I scroll down the content is not showed, nothing is showed. check the picture :
Update
Debug View Hearachy.
As you can see the WebView height in the view hierarchy hasn't changed. Only the web page has changed the size.
We already solved it in the comments, so let's just put this answer for completeness' sake.
If you're using Auto Layout, make sure to change the size of the WebView by setting constraints such as a height constraint and changing its constant to make the layout change. If you want to animate a specific Auto Layout change, do something like this:
[UIView animateWithDuration:0.5 animations:^{
self.webViewHeightConstraint.constant = fittingSize.height;
[self.view layoutIfNeeded];
}];
Kind of a long shot but you might want to try
webView.scrollView.contentSize = fittingSize;
or
webView.scrollView.frame = CGRectMake(0,0,fittingSize.width, fittingSize.height);
Because scrolling was turned off the content size or frame may not be changing after loading the html. Like I said a long shot but might work.
I am using the following code to find the UIWebview height based on the html content, but its not returning exactly correct size. some time it give more size and some times it gives less size.I don't want to show some extra space in uiwebview
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
NSString *desccontentH = [webView stringByEvaluatingJavaScriptFromString:#"document.getElementById(\"desc\").offsetHeight;"];
NSLog(#"Webviewheight: %#", desccontentH);
CGSize fittingSize = [webView sizeThatFits:CGSizeZero];
NSLog(#"sizeThatFits:Height%f, width:%f",fittingSize.width, fittingSize.height);
.....
}
How to find the correct uiwebview height based on the html content?
Take this example used for TextView:
CGFloat descriptionContentHeight = [self sizeOfText:[NSString stringWithFormat:#"%#\n",addressTextView.text] widthOfTextView:addressTextView.frame.size.width withFont:[UIFont fontWithName:#"OpenSans-Light" size:17.5f]].height + 4;
addressTextView.contentInset = UIEdgeInsetsMake(-10,8,0,0);
[addressTextView setFrame:CGRectMake(addressTextView.frame.origin.x,addressTextView.frame.origin.y ,addressTextView.frame.size.width, descriptionContentHeight)];
-(CGSize)sizeOfText:(NSString *)textToMesure widthOfTextView:(CGFloat)width withFont:(UIFont*)font
{
CGSize size = [textToMesure sizeWithFont:font constrainedToSize:CGSizeMake(width-20.0, FLT_MAX) lineBreakMode:NSLineBreakByWordWrapping];
return size;
/// FLT_MAX is definded elsewhere :)
}
Use contentSize property of webView's scrollView.
- (void)webViewDidFinishLoad:(UIWebView *)webView {
CGSize size = webView.scrollView.contentSize;
NSLog(#"width = %f, height = %f", size.width, size.height);
...
}
I have the following UIWebview.
webNieuws = [[UIWebView alloc]initWithFrame:CGRectMake(10, height,300, 400)];
[webNieuws setScalesPageToFit:NO];
[[webNieuws scrollView] setBounces: NO];
webNieuws.delegate = self;
NSString *myDescriptionHTML = [NSString stringWithFormat:#"<html> \n"
"<head> \n"
"<style type=\"text/css\"> \n"
"body {font-family: \"%#\"; size=\"12\" COLOR:#111111; background-color:transparent;}\n"
"</style> \n"
"</head> \n"
"<body>%#</body> \n"
"</html>", #"MyriadPro-Regular",_hotnews.hot_content];
NSLog(#"Hot news content is %#",_hotnews.hot_content);
[webNieuws setBackgroundColor:[UIColor yellowColor]];
[webNieuws setOpaque:NO];
[webNieuws loadHTMLString:myDescriptionHTML baseURL:nil];
[scrollView addSubview:webNieuws];
- (void)webViewDidFinishLoad:(UIWebView *)aWebView {
CGRect frame2 = aWebView.frame;
frame2.size.height = 1;
frame2.origin.y = height;
aWebView.frame = frame2;
CGSize fittingSize = [aWebView sizeThatFits:CGSizeZero];
frame2.size = fittingSize;
CGFloat webHeight = [[aWebView stringByEvaluatingJavaScriptFromString:#"document.documentElement.scrollHeight;"] floatValue];
frame2.size.height =webHeight;
aWebView.frame = frame2;
NSLog(#"Height is %f",webHeight);
float newHeight2 = 30 + webHeight;
NSLog(#"new height header is %f",newHeight2);
[scrollView setFrame:CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height)];
[scrollView setContentSize:CGSizeMake(self.view.frame.size.width,newHeight2 + 320)];
scrollView.bounces = NO ;
}
The same webview gives a different result, like you can see below. I know I can set scalespageTofit. But then I loose my fontsize.
Can someone help me with this ?
Please try this in webViewDidFinishLoad method
NSString *javascriptCommand = [NSString stringWithFormat:#"document.body.style.zoom = 1.0;"];
[aWebView stringByEvaluatingJavaScriptFromString:javascriptCommand];
If this doesnot work then check your webView's autoresing property
I have a list of 15 questions each of them again have sub questions, I'm showing these questions on webView which is present in tableViewCell. Below to this webView there are three labels and three buttons. Now problem is that I'm not getting the heights of tableViewCell based on webView's content. Please suggest me a good solution and thanks in advance.
Here is my code in webviewdidfinishload
- (void)webViewDidFinishLoad:(UIWebView *)aWebView {
int fontSize =160;
NSString *jsString = [[NSString alloc] initWithFormat:#"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '%d%%'", fontSize];
[aWebView stringByEvaluatingJavaScriptFromString:jsString];
CGRect frame = aWebView.frame;
frame.size.height = 1;
aWebView.frame = frame;
CGSize fittingSize = [aWebView sizeThatFits:CGSizeZero];
frame.size = fittingSize;
aWebView.frame = frame;
NSLog(#"size: %f, %f", fittingSize.width, fittingSize.height);
}