iOS : WebView Background color to transparent - ios

What I want is set the background color of webview to transparent.
Below is the code I am using for webview.
NSString *embedHTML = [NSString stringWithFormat:#"<html><body style=\"background-color: transparent;font-family: verdana;\"><h3>About US</h3></BODY></HTML>"];
[webView loadHTMLString:embedHTML baseURL:nil];
However when I use this, I still see background color as WHITE. Any idea how to get color to transparent?

This is all it should take,
[webView setBackgroundColor:[UIColor clearColor]];
[webView setOpaque:NO];

Just add this line.
webView.opaque=NO;

Related

WKWebView pinch-to-zoom in iOS10

I encountered a problem that after I upgraded my phone to iOS 10 beta 3, when I zoom a WKWebView in my app, the zooming function not only zoom the content of the web page but also the page itself.
See the image I attached. WKWebView that fails to zoom correctly. I set the background color of the WKWebView to blue, when I pinch to zoom out the view, the page(container?) itself also zoomed out, causing unfavorable result. It does not happen in iOS9. When I do the same thing, the blue background does not show.
The web page is written in AngularJS and the Graph lib is d3js.
The related code are as below.
-(void) reloadWebPage{
[activityIndicator startAnimating];
NSString *indexToPath = [self getIndexHtmlPath];
NSURL *fileUrl = [NSURL fileURLWithPath:indexToPath];
NSURL *absoluteUrl = fileUrl.absoluteURL;
if(currentGroup == nil){
currentGroup = #"0";
}
NSString *absoluteString = [[absoluteUrl absoluteString] stringByAppendingFormat:#"%#%#", #"?currentGroup=", currentGroup];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:absoluteString] ];
[webView loadRequest:request];
}
- (void)createWebView{
WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
[config.userContentController addScriptMessageHandler:self name:#"interOp"];
webView = [[WKWebView alloc] initWithFrame:self.view.frame configuration:config];
[webView setBackgroundColor:[UIColor blueColor]];
[webView setNavigationDelegate:self];
[webView setUIDelegate:self];
}
I have no clue where is the problem? Is it in Objective C,iOS10 or in Javascript?
Please suggest, How can I resolve this problem?
Thank you
Ok, I resolved this problem by disabling zooming and scrolling in WKWebView.
webView.scrollView.scrollEnabled = false;
- (UIView*) viewForZoomingInScrollView:(UIScrollView *)scrollView{
return nil;
}
After these code are added, the WKWebView itself no longer zooming and moving while the nodes move and zoom normally.
It seems the problem somewhat related to Disable magnification gesture in WKWebView, but
I still don't know what caused the change from iOS9 to iOS10. But it seems to work for me.

UIWebView's content background color not transparent

I'm using an UIWebView to load some HTML text from a plist.
I managed to make its background transparent using
webView.opaque = NO;
webview.backgroundColor = [UIColor clearColor];
but the content, which is the text loaded from the plist, is shown with a disturbing white background.
I'm loading it using
NSString *descr = [NSString stringWithFormat:#"<html><head><style> body{font-size:16; font-family: Helvetica; color: black;} strong{font-family: Arial; font-weight: bold; font-size:16} .bg{background:transparent;}</style></head><body><div class='bg'>%#</div></body></html>",[item valueForKey:#"description"]];
[webView loadHTMLString:descr baseURL:nil];
I've tried setting the .bg class background color to rgba(255,0,0,0.0), but nothing worked.
Also tried setting webView.scrollView.backgroundColor : [UIColor clearColor];
Any advice on how to get the UIWebView's content background transparent?
try this !
[webView setOpaque:NO];
webView.bakgroundColor = [UIColor clearColor];
In XCode 6.x uncheck Opaque and change Background's opacity to 0%. I think other XCode versions will also work.
Your HTML body will still have a background color.
You need to add 'background-color:transparent;' to the body part of your CSS
See here: https://stackoverflow.com/a/3935033/78496

Is there a way to vertically fit web page in UIWebView?

I need that my UIWebView shows me an entire web page, from the top to the bottom (it doesn't matter if the page is very long and I don't care if I'm not able to read it, I just want to see ALL the page).
So, how do I vertically fit the page in the web view?
Scales to fill, Aspect fit etc... don't work.
Thanks.
You can do it simply by using the following line of code:
myWebView.scalesPageToFit = YES;
This will scale the page to adjust both horizontally and vertically within your UIWebView.
You can sacle with javascript function
NSString *jsCommand = [NSString stringWithFormat:#"document.body.style.zoom = 1.5;"];
[myWebView stringByEvaluatingJavaScriptFromString:jsCommand];
[webview setScalesPageToFit:YES];
or
[webview sizeToFit];
setScalesPageToFit: gives output like this
Sometimes myWebView.scalesPageToFit = YES; doesn't work so we have to set it that's why I written [myWebView setscalesPageToFit:YES]; I have faced this type of problem in UIButton that button.text = #"long text"; does not work but when I use [button setTitle:#"long text" forControlState:UIcontrolStateNormal]; it worked. So may be it will work for you

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;

Content mode issue in UIImageView when its set to ASpectFill

I need to load an image in a cell and i get a white border along my image.I'm calling the image from the link and it looks like this.
but my image looks like
i'm getting a white border in my image left and right side.Here is my code for image
UIImageView *item=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 115, 130)];
NSString *imgstring=[NSString stringWithFormat: #"%#",[[mutarray objectAtIndex:indexPath.row ] valueForKey:#"imgurl"]];
NSURL *imgurl=[NSURL URLWithString:imgstring];
UIImage *image=[UIImage imageWithData:[NSData dataWithContentsOfURL:imgurl]];
[item setBackgroundColor:[UIColor clearColor]];
item.contentMode=UIViewContentModeScaleToFill;
[item setImage:image];
[cell.contentView addSubview:item];
Guidance please...
Those white side borders are in the image you posted in your question....that's why they appear in iOS as well. Open that picture in a photo editor and you will see.
You can solve it by editing webimage becaust it has white background in that u can add the following code to differentiate the image by giving
item.backgroundcolor=[UIColor blackcolor];
Hope it helps..

Resources