Horizontal Scrolling in UIWebView Not Working - ios

I've looked around for a while and there seems to be quite a few questions on scrolling in UIWebView, but none of the solutions have worked for me. I have a UIWebView inside a view on one of the pages of my app:
myWebView = [[UIWebView alloc] initWithFrame:b];
[myWebView.scrollView setContentSize:CGSizeMake(1136, 400)];
[myWebView loadHTMLString:styleSheet baseURL:nil];
[myView addSubview:myWebView];
Right now, scrolling vertically works fine, but changing contentSize seems to not affect anything.
I need to display a small spreadsheet and making the view wider will make it much more readable.

To set the height of the webView page in HTML do this:
myWebView = [[UIWebView alloc] initWithFrame:b];
[myWebView loadHTMLString:styleSheet baseURL:nil];
[myView addSubview:myWebView];
where styleSheet is a NSString with this HTML Code:
NSString *styleSheet = [NSString stringWithFormat:#"html { min-width: 100%%; min-height: 100%%; } body { width: %dpx; height: %dpx; background-color: #FFF; }", myWebView.frame.size.width*2, myWebView.frame.size.height*2];
(width*2 and height*2 for retina display)
You can of course add anything you want to the HTML string to change the content of the webView.

Related

Cropping a UIWebView in Xcode

I have a UIWebView, but don't want to display anything at the top of the page, down to about 50 pixels. I need a way to crop the UIWebView so that users cannot see what is above that 50 pixel margin. How can I go about doing this effectivly?
UIWebview contains a UIScrollView which is used to display HTML content. Changing the conent offfset allow to change the page view... see example of google
/**
* basic webview boilerpoint
*/
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0,
0,
CGRectGetWidth(self.view.frame),
CGRectGetHeight(self.view.frame))];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://google.co.uk"]]];
[self.view addSubview:webView];
/**
* disable bounces so users cannot scroll beyond content & set top offset by 100 to hide parts of website
*/
webView.scrollView.bounces = NO;
webView.scrollView.contentInset = UIEdgeInsetsMake(-100, 0, 0, 0);

scrolling in the bottom is not working

I am using a web view its working fine. but I am having a three issue 1.i want to change the font 2. change the size of font 3. when I scroll the web content to bottom it's not scrolling up to the bottom.
content=[[UIWebView alloc]init];
content.Frame=CGRectMake(10, 200, 300, 480);
[content loadHTMLString:content1 baseURL:nil];
[content loadHTMLString:[NSString stringWithFormat:#"<html><body background-colo r: transparent text=\"#eadfa8\" font-family=\"Arial Rounded MT Bold\" font-size=\".1\">%#</body></html>", [self.JSONData objectForKey:#"content"]] baseURL: nil];
[content stringByEvaluatingJavaScriptFromString: #"content.body.style.fontFamily = 'Arial'"];
content.opaque=YES;
[self.view addSubview:content];
[content setBackgroundColor:[UIColor clearColor]];
[content setOpaque:NO];
above html coding I try to change the font family to Arial but its not working. help me .
For First two issues answer is Font change inside UIWebView
The third issue you can overcome by below lines
content.scalesPageToFit=TRUE;
content.scrollView.bounces = NO;

UIWebview full screen size

I am trying to display a pdf in a UIWebview using the entire screen of the device, except the status bar and top bar. So far, I created IBOutlet UIWebview *webview; in my .h file. I connected the webview in my storyboard and wrote the following code in my .m file:
- (void)viewDidLoad
{
[super viewDidLoad];
webview = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
webview.scalesPageToFit = YES;
webview.autoresizesSubviews = YES;
webview.autoresizingMask=(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);
[webview setBackgroundColor:[UIColor clearColor]];
NSString *path = [[NSBundle mainBundle] pathForResource:#"Chart" ofType:#"pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webview loadRequest:request];
[self.view addSubview:webview];
}
The pdf displays correctly in my 3.5 inch, but in my 4 inch display, the bottom is cut off. This is even more noticeable when I rotate to landscape view. About 33% of the screen is not used at all. How can I fix this? I know it must have something to do with the UIWebview dimensions in my code above (webview = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];), but is there a better way to simply display the webview on the entire screen, without specifying a specific screen height and width?
One cheap thing to do would be to set the frame of the webview in overriding - (void)viewDidLayoutSubviews
- (void)viewDidLayoutSubviews {
webView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
}
the advantage of doing it here, is that the size and orientation of the view has already been determined here, and you can get an accurate measurement.
Using Auto Layout
Be sure the "Constrain to margins" option is not checked, all constrain values are 0:
And your webview is a first child of your main view:
Without Auto Layout
Change webview autosizing connections:
After a lot of headache dealing with compatibility of iOS6, 7 and different screen sizes, I use this one:
- (void)viewDidLoad {
[super viewDidLoad];
self.webview = [[UIWebView alloc] init];
// More setting your webview here
// Set webview to full screen
self.view = self.webview;
[self.webview loadRequest:aRequest];
}
I was able to get this working correctly for me by selecting the Reset to Suggested Constraints under the Resolve Auto Layout Issues menu.

Set font size content UIWebview

I built an application to read mail on the ipad, when displayed html body content to UIWebView the font size it is not uniform, I can not adjust the font size as all the other applications MailBox, 'Gmail'..
I use:
html {
-webkit-text-size-adjust: none; /* Never autoresize text */
}
but does not solve the problem, how to fix it?
try this one it worked for me
- (void)webViewDidFinishLoad:(UIWebView *)wView {
[wView stringByEvaluatingJavaScriptFromString:#"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '50%'"];
[wView stringByEvaluatingJavaScriptFromString:#"document.getElementsByTagName('body')[0].style.zoom= '0.5'"];
}
You can set font size of webview , like
NSString *fontsize;
fontsize=20px;
NSString *WebviewString;
WebviewString=#"xyz";
Put the value of fontsize and WebviewString in this:
[MywebView loadHTMLString:[NSString stringWithFormat:#"<div style='text-align:justify; font-size:%#;font-family:Helvetica;color:#ffff;'>%#",fontsize,WebviewString] baseURL:nil];
Hope it will helpyou.

How to size UIWebView in a UIModalPresentationFormSheet?

I tried every permutation known to mankind and I cannot get a UIWebView to size properly in a modal form view for the iPad. The web page still displays the full iPad portrait width of 768 points.
Where and how do I tell UIWebView to display width 540 points?
I thought 'scalesPageToFit' is supposed to do this but it does not work.
I tried setting the web view to the size of form view which is 540 x 576 with navigation and status bar. Height is irrelevant though.
I tried adding a UIWebView to a UIView in a storyboard with all resizing set. I then removed the UIWebView and added it programmatically.
- (void)viewDidLoad
{
[super viewDidLoad];
CGRect aFrame = self.view.frame;
if (IS_IPAD)
{
aFrame = CGRectMake(0, 0, FORM_VIEW_WIDTH, FORM_VIEW_HEIGHT);
}
_webView = [[UIWebView alloc] initWithFrame:aFrame];
[_webView setOpaque:NO];
[_webView setDelegate:self];
[_webView setScalesPageToFit:YES];
self.view = _webView;
...
}
I also tried loading in viewDidAppear (in addition to viewDidLoad, viewWillAppear, etc.)
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[[self webView] setFrame:CGRectMake(0, 0, FORM_VIEW_WIDTH, FORM_VIEW_HEIGHT)];
[[self webView] setScalesPageToFit:YES];
NSURL *webURL = [NSURL URLWithString:#"http://www.google.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:webURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:5.0f];
[[self webView] loadRequest:request];
}
Any recommendations appreciated.
This keeps happening to me. I post a lengthy question, then two seconds later I find the answer.
Anyway, someone posted a solution to this problem here:
UIWebView -- load external website, programmatically set initial zoom scale, and allow user to zoom afterwards
However, I altered the script to set the width to the iPad width:
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
NSString* js =
#"var meta = document.createElement('meta'); "
#"meta.setAttribute( 'name', 'viewport' ); "
#"meta.setAttribute( 'content', 'width = 540px, initial-scale = 1.0, user-scalable = yes' ); "
#"document.getElementsByTagName('head')[0].appendChild(meta)";
[[self webView] stringByEvaluatingJavaScriptFromString: js];
}
Note the portion 'width = 540px, initial-scale = 1.0, user-scalable = yes'
I changed the width to the form width and scale to 1.0.
I also need to handle iPhone, and so I will make adjustments for that next. I think this fix may only be needed for the iPad form view.
UPDATE: Answer is short-lived. If I load any other page the sizing is thrown off again. Back to square one.
SOLUTION: This view will only need to access a few web pages that I developed. Therefore, I updated the few web pages that I need to access by adding the meta tag to each of them. I only need to access these pages and so this is the solution I plan to go with. This means I need to set the viewport width to 540px only for iPad somehow. I will figure that out next.
<meta name="viewport" content="width:540, initial-scale=1, maximum-scale=1">

Resources