zoomed-in via button click on web view iPad app - ipad

In web view my code is`static float i=1.5;
if(i<=7.5)
{
[btnZoom setEnabled:YES];
objWebView.transform = CGAffineTransformMakeScale(1+i,1+i);
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.0f];
[UIView commitAnimations];
i=i+1.5;
NSLog(#"i===============%f",i);
}
if(i==7.5)
{
NSLog(#"i===============%f",i);
[btnZoom setEnabled:NO];
i=1.5;
}
objWebview is the object of web view.my scales page to fit property of webview is clicked but I have to zoom in on the button click. (I am new in iPhone)
By this code it is getting zoomed-in, but my webview contents are getting blurred when zoomed in.

There are two ways to zoom your UIWebView by codebase.
Zoom UIScrollView available in UIWebView
This option will applicable from iOS 5.0 and >
- (void)viewDidLoad
{
NSString *urlAddress = #"<website url address here>";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url]
[webView loadRequest:requestObj];
webView.delegate = self;
webLookupView.scalesPageToFit = YES;
}
- (void)webViewDidFinishLoad:(UIWebView *)theWebView
{
CGSize contentSize = theWebView.scrollView.contentSize;
CGSize viewSize = self.view.bounds.size;
float rw = viewSize.width / contentSize.width;
theWebView.scrollView.minimumZoomScale = rw;
theWebView.scrollView.maximumZoomScale = rw;
theWebView.scrollView.zoomScale = rw;
}
Zoom Webpage itself
In UIWebViewDelegate method – webViewDidFinishLoad:
NSString *jsCommand = [NSString stringWithFormat:#"document.body.style.zoom = 1.5;"];
[theWebView stringByEvaluatingJavaScriptFromString:jsCommand];
Hope this helps you.

Related

xcode 8 created a second controller as a popup but just get black screen

I've tried using similar answers to my problem that i've encounter. I'm new to Xcode and I have one controller that contains a uiwebview that loads my website. i have coding to detect if a certain link is pressed. The issue i'm running into is when a certain link is pressed, I need my second controller to act as a pop up and display a different webpage. Everything works and that link is pressed my NSLog says that it in the second controller but the display is black. Here code that i have in the main view controller that loads my webpage:
- (void)viewDidLoad {
[super viewDidLoad];
self.webViewer.delegate=self;
NSString *fullURL = #"https://www.example.com?first_run=true";
NSURL *url = [NSURL URLWithString:fullURL];
//NSLog(#"first page loaded=%#",url);
NSURLRequest *requestObj = [NSURLRequest requestWithURL: url];
[_webViewer loadRequest:requestObj];
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
self.webViewer.delegate=self;
NSURL *strurl = request.URL;
NSString *urlString = strurl.relativeString;
NSLog(#"link clicked=%#",urlString);
if(//not the right link do this){
}
else{//is the right link do this
NSLog(#"found");
self.popup = [[PopUpController alloc] init];
[self.popup showInView:self.view animated:YES];
return NO;
}
}
#end
then for my second controller it's supposed to load a different webpage but just get a black screen:
- (void)viewDidLoad {
self.popupView.delegate=self;
NSString *popupURL = #"https://www.example2.com";
NSURL *url_popup = [NSURL URLWithString:popupURL];
NSLog(#"popup page loaded=%#",url_popup);
NSURLRequest *requestObj2 = [NSURLRequest requestWithURL: url_popup];
[_popupView loadRequest:requestObj2];
}
- (void)showAnimate{
self.view.transform = CGAffineTransformMakeScale(1.3, 1.3);
self.view.alpha = 0;
[UIView animateWithDuration:.25 animations:^{
self.view.alpha = 1;
self.view.transform = CGAffineTransformMakeScale(1, 1);
}];
}
- (void)showInView:(UIView *)aView animated:(BOOL)animated{
CGFloat x = self.view.center.x - (aView.frame.size.width);
CGFloat y = self.view.center.y - (aView.frame.size.height);
[aView setFrame:CGRectMake(x,y,aView.bounds.size.width,aView.bounds.size.height)];
[aView addSubview:self.view];
if (animated) {
[self showAnimate];
}
}
Any help would be extremely appreciated. I've tried for 3 weeks searching and trying different solutions found on this forum but they all didn't work for mine. Not sure if I'm just not instantiating my controllers right or some piece of code that I'm missing. Thank you for your time in advanced.
This line:
self.popup = [[PopUpController alloc] init];
simply creates an instance of PopUpController but doesn't load any of you UI elements with it.
Assuming you have designed PopUpController in storyboards, give it an Identifier such as "MyPopUp"
and then use:
// storyboard reference
UIStoryboard *storyboard = [UIStoryboard storyboardWithName: #"Main" bundle:[NSBundle mainBundle]];
// instantiate PopUpController from the storyboard
self.popup = (PopUpController *)[storyboard instantiateViewControllerWithIdentifier:#"MyPopUp"];
// show it
[self presentViewController:self.popup animated:YES completion:nil];

Xcode UIWebView not changing page with changed URL

I'm automatically generating a NSURL based on the distance of a pan gesture on the screen (long story...). Anyways, I'm having no problem generating the URL. The URL dynamically changes, and then I have an NSTimer that repeats every second which tells a UIWebView to reload with the new URL.
However, although the URL is reported to be changing, the UIWebView will only show the first URL sent to it. I'm also monitoring the website itself, and it's receiving the first URL every second, opposed to the updated ones. My code is pretty simple right now, so I'm not sure what I did wrong? Any help would be appreciated.
In ViewController.h:
#property (weak, nonatomic) IBOutlet UIWebView *webView;
#property float slicenumber;
#property NSURL *dynamicUrl;
In ViewController.m:
- (void)viewDidLoad {
[super viewDidLoad];
NSString *fullURL = #"http://localhost:8080/remote/slicer/slice?view=Green&orientation=Axial&scrollTo=.50&size=native&fmt=png";
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:requestObj];
[self.webView reload];
self.webView.scrollView.scrollEnabled = NO;
self.webView.scrollView.bounces = NO;
self.slicenumber=.5;
[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(UpdateSlice) userInfo:nil repeats:YES];}
- (IBAction)handlePan:(UIPanGestureRecognizer *)recognizer {
CGPoint translation = [recognizer translationInView:self.view];
[recognizer setTranslation:CGPointMake(0, 0) inView:self.view];
self.slicenumber=self.slicenumber+translation.y/350;
if (self.slicenumber<0) {
self.slicenumber=0;
}
if (self.slicenumber>1) {
self.slicenumber=1;
}
self.sliceloc.text= [NSString stringWithFormat:#"%.2f",self.slicenumber];
NSString *fullURL2 = [NSString stringWithFormat:#"http://localhost:8080/remote/slicer/slice?view=Green&orientation=Axial&scrollTo=%.2f&size=native&fmt=png",self.slicenumber];
self.dynamicUrl = [[NSURL alloc] initWithString:[fullURL2 stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]];
NSLog(#"urlString = %#",self.dynamicUrl);}
- (void)UpdateSlice {
[self.webView loadRequest:[NSURLRequest requestWithURL:self.dynamicUrl]];
[self.webView reload];}
Thanks a lot! Let me know if you need any other info.
[self.webView reload] - will reload the current page. This is probably happening before the loadRequest has finished.
Try removing this line.
Also, #joern's comment is correct; the 'event' is the user making a pan gesture. Lose the timer.

WebView height not set based on its content

I know this question was posted so many times but still I can not change the height of UIWebView based on its content
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.title=#"webview";
[_webView setDelegate:self];
NSString * string = #"<h1>This is HTML string</h1>";
[_webView loadHTMLString:string baseURL:nil];
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
return YES;
}
- (void)webViewDidStartLoad:(UIWebView *)webView
{
NSLog(#"start loadinng.......");
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
NSLog(#"finish loading.......");
CGRect frame = _webView.frame;
frame.size.height = 1;
_webView.frame = frame;
CGSize fittingSize = [_webView sizeThatFits:CGSizeZero];
frame.size = fittingSize;
_webView.frame = frame;
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
NSLog(#"Failed to load with error :%#",[error debugDescription]);
}
can anybody help me how can resize my UIWebView height?
Here is your answer.
- (void)webViewDidFinishLoad:(UIWebView *)webView {
if (webView.isLoading){
return;
}
CGSize contentSize = webView.scrollView.contentSize;
NSLog (#"height: %f",contentSize.height);
}
After gettting this height reframe your webView with new width and height and also reframe its parent view if there any.
How about this.
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
[webView sizeToFit];
webView.frame = CGRectMake(x,y,width,webView.frame.size.height);
}
I usually use these methods, to set UIWebview frame as it's content size:
- (void)webViewDidStartLoad:(UIWebView *)webView
{
CGRect frame = webView.frame;
frame.size.height = 5.0f;
webView.frame = frame;
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
CGSize mWebViewTextSize = [webView sizeThatFits:CGSizeMake(1.0f, 1.0f)]; // Pass about any size
CGRect mWebViewFrame = webView.frame;
mWebViewFrame.size.height = mWebViewTextSize.height;
webView.frame = mWebViewFrame;
//Disable bouncing in webview
for (id subview in webView.subviews)
{
if ([[subview class] isSubclassOfClass: [UIScrollView class]])
{
[subview setBounces:NO];
}
}
}
They are automatically called (if you set webviews delegate to this class), when WebView has finished loading it's content.
or
check this
raywenderlich-tutorial
This might helps you :)
First calculate height of web view for desired content.
NSString* lString = #"<h1>This is HTML string</h1>";
NSAttributedString* lAttributedText = [[NSAttributedString alloc] initWithData: [lString dataUsingEncoding:NSUTF8StringEncoding]
options:#{
NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
NSCharacterEncodingDocumentAttribute: #(NSUTF8StringEncoding)
}
documentAttributes: nil
error:nil];
CGRect lBoundingRect = [lAttributedText boundingRectWithSize: CGSizeMake(_webView.bounds.size.width, CGFLOAT_MAX) options: NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading context: nil];
then set it to _webView.frame
Now, load web view
[_webView setDelegate:self];
[_webView loadHTMLString: lString baseURL:nil];

How to setup the UIWebView to make the page auto zoom in and fit in whole window?

I use UIWebView to load a html page which contains only one SVG image file with below code:
self.webView = [UIWebView new];
self.webView.opaque = YES;
self.webView.frame = self.view.frame;
[_webView setAutoresizingMask:(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth)];
self.webView.dataDetectorTypes = UIDataDetectorTypeAll;
self.webView.userInteractionEnabled = YES;
_webView.scalesPageToFit = YES;
self.webView.delegate=self;
[self.view addSubview:self.webView];
and I take the a screen capture as below, I want to make the image file can be auto zoom in to fit the whole window when View is shown, currently I had to double tab the image to make it happen.
and belwo is what I expected:
Found the answer
NSString *jsCommand = [NSString stringWithFormat:#"document.body.style.zoom = 1.5;"];
[_webview stringByEvaluatingJavaScriptFromString:jsCommand];

UIWebView content how to resize/scale it?

I came to a point where I was able to lock the horizontal scroll as I wanted however when I rotate from landscape to portrait the content is still to large. I need to resize/scale it down myself but I don't know how to so far I use this :
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *fullURL = #"http://igo.nl";
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[_webView loadRequest:requestObj];
_webView.scrollView.bounces = false;
_webView.scalesPageToFit = YES;
_webView.contentMode = UIViewContentModeScaleAspectFit;
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
if(fromInterfaceOrientation == UIInterfaceOrientationPortrait)
{
//_webView.scrollView.contentSize = CGSizeMake(320, _webView.scrollView.contentSize.height);
}
else
{
//[_webView reload];
_webView.bounds = CGRectMake(0, 0, 320,_webView.bounds.size.height);
_webView.frame = CGRectMake(0, 0, 320, _webView.frame.size.height);
_webView.scrollView.contentSize = CGSizeMake(320, _webView.scrollView.contentSize.height);
}
}
EDIT:
What I seem to need is that the content is zoomed out
Use the delegate methods of UIWebViewDelegate
#interface YOUR_CLASS : PARENT_CLASS <UIWebViewDelegate>
In viewDidLoad
...
[_webView setDelegate:self];
...
Call the delegate method
- (void)webViewDidFinishLoad:(UIWebView *)webView {
[webView.scrollView setContentSize: CGSizeMake(webView.frame.size.width, webView.scrollView.contentSize.height)];
}
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
[_webView reload]; //Or use meta tag
[_webView.scrollView setContentSize: CGSizeMake([_webView.scrollView.contentSize.width, _webView.scrollView.contentSize.height)];
}
You have to add meta tag to your HTML file
<meta name="viewport" content="width=device-width" />
or simply reload the webView
[_webView reload];
try in
-(void)viewWillLayoutSubviews
{
------
}
calculate using
self.view.bounds.size.width,
self.view.bounds.size.height
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
if(toInterfaceOrientation==UIInterfaceOrientationPortrait && toInterfaceOrientation!=UIInterfaceOrientationPortraitUpsideDown )
{
}
else if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation ==UIInterfaceOrientationLandscapeRight)
{
}
}

Resources