How to get content width of uiwebview? - ios

I load Gif image like this
[webView2 loadData:[NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"Apple_jumps" ofType:#"gif"]] MIMEType:#"image/gif" textEncodingName:nil baseURL:nil];
and I want to set to center position of uiwebview, but I can't to get width of content. Please help.

You can use document.height or document.width from javascript
// in init
_WebView.delegate = YourClass; // probably self
- (void) webViewDidFinishLoad:(UIWebView *)webview
{
int w = [[_WebView stringByEvaluatingJavaScriptFromString:#"document.width"] floatValue];
_WebView.userInteractionEnabled = w > 200;
}
more info here: https://www.albertopasca.it/whiletrue/objective-c-get-uiwebview-html-page-size/

Related

How to center a small size svg icon in a larger size webview?

I am using a Webview to load svg icons in my app. It works well with big images by using scalesPageToFit property. Now I am facing issues when using small size icons to fit in webview frame.
e.g. -
WebView size - 150w*150h
Icon dimension - 30*30
In this situation, icons align to the left top position of the webview. Is there any suggestion to center this in the web view or to enlarge it within webview frame? Any suggestion?
NSURL *imageURL = [NSURL URLWithString:#"...badge.svg"];
NSURLRequest *request = [NSURLRequest requestWithURL:imageURL cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval: 10.0];
[self.menuView.webView loadRequest:request];
self.menuView.webView.delegate = self;
//Delegate method
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
CGSize contentSize = webView.scrollView.contentSize;
CGSize webViewSize = webView.bounds.size;
CGFloat scaleFactor = webViewSize.width / contentSize.width;
webView.scrollView.minimumZoomScale = scaleFactor;
webView.scrollView.maximumZoomScale = scaleFactor;
webView.scrollView.zoomScale = scaleFactor;
webView.scrollView.scrollEnabled = NO;
}
Thanks!
Try this;
NSString *bodyStyleVertical = #"document.getElementsByTagName('body')[0].style.verticalAlign = 'middle';";
NSString *bodyStyleHorizontal = #"document.getElementsByTagName('body')[0].style.textAlign = 'center';";
[webView stringByEvaluatingJavaScriptFromString:bodyStyleVertical];
[webView stringByEvaluatingJavaScriptFromString:bodyStyleHorizontal];

UIWebView with dynamic height inside a UIScrollView in Objective-C

I am trying to make a UIWebView with "dynamic screen height". This webview is inside a UIScrollView, below other components. See the layout (it uses autolayout):
My idea is to provide just one scroll (from UIScrollView - its working) and make the WebView viewport grow depending the content size. And its not working.
What I did to try to do this:
UIWebView property "scale page to fit " is on;
UIWebView is unpaginated;
UIWebView does not allow user interaction.
In my UIViewController:
- (void) viewDidAppear:(BOOL)animated {
NSString *urlString = #"MY URL GOES HERE";
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[_webViewDescription loadRequest:urlRequest];
_webViewDescription.delegate = self;
_webViewDescription.scrollView.scrollEnabled = NO;
_webViewDescription.scrollView.bounces = NO;
}
My UIWebViewDelegate:
- (void)webViewDidFinishLoad:(UIWebView *)webView {
CGRect frame = webView.frame;
frame.size.height = 1;
webView.frame = frame;
CGSize fittingSize = [webView sizeThatFits:CGSizeZero];
frame.size = fittingSize;
webView.frame = frame;
NSLog(#"size: %f, %f", fittingSize.width, fittingSize.height);
_myScrollView.contentSize = webView.bounds.size;
}
When I run the code, it prints the correct UIScrollView size and UIWebView size. The UIScrollView height got bigger, but the UIWebView maintains the same height from the first loading.
I am testing in a real device.
add a height constraint to your webView and make a IBOutlet of that like
#property(strong, nonatomic) IBOutlet NSLayoutConstraint *webViewHeightConstraint;
load your webview and in web view delegate
- (void)webViewDidFinishLoad:(UIWebView *)webView
get webview content height and set webViewHeightConstraint.constant
like below:-
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
NSString *str = [webView stringByEvaluatingJavaScriptFromString:#"(document.height !== undefined) ? document.height : document.body.offsetHeight;"];
CGFloat height = str.floatValue;
webViewHeightConstraint.constant = height;
}
hope it may help you.
Try to set webView.frame = frame; in viewDidLayoutSubviews()
For setting dynamic height for UIWebView, you need to set the height in webViewDidFinishLoadView method
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
CGFloat height = [[webview stringByEvaluatingJavaScriptFromString:#"document.height"] floatValue];
//For Width
//CGFloat width = [[webview stringByEvaluatingJavaScriptFromString:#"document.width"] floatValue];
CGRect frame = webview1.frame;
frame.size.height = height;
//frame.size.width = width; //Width
webview.frame = frame;
CGRect screenBounds = [UIScreen mainScreen].bounds ;
CGFloat widthForIpad = CGRectGetWidth(screenBounds) ;
CGFloat heightForIpad = CGRectGetHeight(screenBounds) ;
NSLog(#"The iPad width is - %fl",widthForIpad);
NSLog(#"The iPad height is - %fl",heightForIpad);
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
if ([[UIScreen mainScreen] bounds].size.height == 568)
scrollView.contentSize = CGSizeMake(320, height+370); //set your required height
else
scrollView.contentSize = CGSizeMake(320, height+350); //set your required height
}
else
{
if ([[UIScreen mainScreen] bounds].size.height == 1024)
scrollView.contentSize = CGSizeMake(320, height+370); //For iPad
}
}
You can get the content size of webview content using webView.scrollView.contentSize.height.
and than you can use this webView.scrollView.contentSize.height to set the contentSize of the scrollview inside webViewDidFinishLoad method.
Like this in webViewDidFinishLoad method
[objWebView setFrame:CGRectMake(objWebView.frame.origin.x, objWebView.frame.origin.y, objWebView.frame.size.width, webView.scrollView.contentSize.height)];
if(objWebView.frame.origin.y+objWebView.frame.size.height > objScrollView.contentSize.height) {
[objScrollView setContentSize:CGSizeMake(objScrollView.frame.size.width, objWebView.frame.origin.y+objWebView.frame.size.height)];
}

UiWebview in IPhone resize as per the content size

I am having an issue like i am using UIWebView in Iphone and data coming as HTML am storing the html data as astring and passing to the UIWebView am getting it in good but i want to Change the UIWebView Size as per the content size
- (void)viewDidLoad {
web.delegate = self;
NSString * str = [dict objectForKey:#"terms"];
[web loadHTMLString:str baseURL:nil];
}
- (void)webViewDidStartLoad:(UIWebView *)webView
{
CGRect frame = web.frame;
frame.size.height = 1;
CGSize fittingSize = [web sizeThatFits:CGSizeZero];
frame.size = fittingSize;
web.frame = frame;
NSLog(#"size: %f, %f", fittingSize.width, fittingSize.height);
web.frame = CGRectMake(10, 100, web.scrollView.contentSize.width, web.scrollView.contentSize.height);
}
set your web view delegate and frame.
yourwebview.frame=[[UIScreen mainScreen] bounds];
yourwebview.delegate = self;
end then use this delegate method to set height.
- (void)webViewDidFinishLoad:(UIWebView *)aWebView {
CGSize contentSize = aWebView.scrollView.contentSize;
NSLog(#"webView contentSize: %#", NSStringFromCGSize(contentSize));
yourwebview.contentsize = contentSize;
}
You could use the delegate method webViewDidFinishLoad: but sometimes this is called before the HTML is fully rendered and that leads to wrong height values.
To make this work reliably you have to wait until the HTML is fully rendered and then use Javascript to send the correct height to your UIWebView.
Please have a look at this blogpost I wrote some time ago.
Do it with your web view's delegate method :
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
webView.frame = CGRectMake(0, 0, webView.scrollView.contentSize.width, webView.scrollView.contentSize.height);
}

iOS - Playing video through UIWebView: Player controls appear in awkward position

I'm trying to play a .m3u8 through a webView inside a HTML video tag.
The video plays correctly and has the functionality I need. But the presentation is off... The player controls appear on the bottom of the video, overlayed half-way on and half-way off the video content.
(Sorry, massive screenshots)
Here, I extended the frame for the webview, and you can see that the video stops, but the controls appear lower.
Does anyone know if there is a way to reposition the player's controls?
Here's my webview code:
- (void)viewDidAppear:(BOOL)animated {
_webView.delegate = self;
_webView.allowsInlineMediaPlayback = YES;
NSString *urlAddress = #"http://www.pulpwoodpress.com/playVidTest.html";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[_webView loadRequest:requestObj];
_webView.scrollView.scrollEnabled = NO;
}
- (void)webViewDidFinishLoad:(UIWebView *)theWebView
{
NSLog(#"%f, %f", theWebView.scrollView.contentSize.width, theWebView.scrollView.contentSize.height);
CGSize contentSize = CGSizeMake(theWebView.scrollView.contentSize.width, theWebView.scrollView.contentSize.height);
CGSize viewSize = self.view.bounds.size;
float rw = viewSize.width / contentSize.width;
theWebView.scrollView.minimumZoomScale = rw;
theWebView.scrollView.maximumZoomScale = rw;
theWebView.scrollView.zoomScale = rw;
theWebView.scrollView.contentOffset = CGPointMake(0, 17);
}

How to get actual web page size (in Byte or KB or MB) of a Loaded UIWebView? [duplicate]

I'm using a UIWebView for displaying content, in the form of an HTML string – not a website, higher than the screen of the iPhone, without needing to scroll in the webView itself, leaving that to the parent scrollView.
To achieve this, I need a way to get the total document size, including the scrollable area, to set the webView's height. I have tried a number of different Javascript solutions:
(document.height !== undefined) ? document.height : document.body.offsetHeight // Returns height of UIWebView
document.body.offsetHeight // Returns zero
document.body.clientHeight // Returns zero
document.documentElement.clientHeight // Returns height of UIWebView
window.innerHeight // Returns height of UIWebView -2
document.body.scrollHeight // Returns zero
Is there a solution that actually works?
Current (nonworking) code:
[[[self.singlePost.contentText subviews] lastObject] setScrollEnabled:NO];
int content_height = [[self.singlePost.contentText stringByEvaluatingJavaScriptFromString: #"document.body.offsetHeight"] intValue];
NSLog(#"Content_height: %d", content_height);
CGRect rect = self.singlePost.contentText.frame;
rect.size.height = content_height;
self.singlePost.contentText.frame = rect;
There is no need to use Javascript in iOS 5.0 and up - you have direct, documented access to its scrollView:
- (void)webViewDidFinishLoad:(UIWebView *)webView {
CGFloat contentHeight = webView.scrollView.contentSize.height;
// ....
}
To get the total height of the contents of the webView.
When I tried with my code, I found,
(1) NSLog(#"webView height: %#", [webView stringByEvaluatingJavaScriptFromString: #"document.body.offsetHeight"]);
(2) NSLog(#"webView height: %#", [webView stringByEvaluatingJavaScriptFromString: #"document.height"]);
(1) return the height less than the original height. But (2) return the actual height. My suggestion is to get the Max of the two.
- (void)webViewDidFinishLoad:(UIWebView *)theWebView {
NSLog(#"Body height: %#", [webView stringByEvaluatingJavaScriptFromString: #"document.body.offsetHeight"]);
NSLog(#"Doc height: %#", [webView stringByEvaluatingJavaScriptFromString: #"document.height"]);
NSString * javaScript = [NSString stringWithFormat:#"document.getElementById('%#').clientHeight", kDivID];
NSLog(#"Div height: %#",[webView stringByEvaluatingJavaScriptFromString:javaScript]);
[self reLayout];
}
- (CGFloat) getWebViewPageHeight {
CGFloat height1 = [[webView stringByEvaluatingJavaScriptFromString: #"document.height"] floatValue];
CGFloat height2 = [[webView stringByEvaluatingJavaScriptFromString: #"document.body.offsetHeight"] floatValue];
return MAX(height1, height2);
}
Output
Body height: 485
Doc height: 509
Div height: 485
Where do you call your code?
For me it returns 0 if it is called right after the loadHTMLString Method.
If I call it in the (void)webViewDidFinishLoad:(UIWebView *)theWebView delegate, I get a valid value.
- (void)loadHTML: (NSString *)html {
webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
webView.delegate = self;
NSURL *resourceURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
[webView loadHTMLString:html baseURL: resourceURL];
NSLog(#"height: %d", [[webView stringByEvaluatingJavaScriptFromString: #"document.body.offsetHeight"] intValue]); // returns 0
}
- (void)webViewDidFinishLoad:(UIWebView *)theWebView {
NSLog(#"height: %#", [webView stringByEvaluatingJavaScriptFromString: #"document.body.offsetHeight"]); //return 2166
}
I went with a slightly different approach, to create a <DIV> that I could key off of to get the size, because I was having no luck with inspecting document/body.
This is MonoTouch C#, but should work fine in Objective-C:
private const string DIV_ID = "_uiwebview_";
LoadHtmlString("<body style='background-color:#yourcolor; padding:0px; margin:0px'>" +
"<div style='width:" + Frame.Width + "px; padding:0px; margin:0px; font-family:" +
font.Name + "' id=" + DIV_ID + ">" + html + "</div></body>", base_url);
and getting the height in LoadFinished:
string sheight = EvaluateJavascript("document.getElementById('" + DIV_ID +
"').clientHeight");
#Jesse, this works when updating with HTML that would generate a smaller size than was previously shown.

Resources