Set font size content UIWebview - ios

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.

Related

How can I scale the fixed size html content to fit the UIwebview frame size?(the content size is much smaller than the frame size)

I am now doing a advertSDK project like inmobi, the user is allowed to create the banner frame and locate them by CGrectmake, so I will create a UIWebview as they required, then in the UIwebview I just to load the html content to show it in the right position.
The question is: the banner content has a fixed size : 360 * 100, however in iphone 6s simulator, it is too small, I tried to set
webView.scalesPageToFit = YES
but it has no effects.
Anyone has a good idea to solve the problem? Thank you.
You have to use the UIWebView delegate methods and resize the web view like I'm doing here:
- (void) webViewDidFinishLoad:(UIWebView *)webView {
webView.frame = CGRectMake(webView.frame.origin.x, webView.frame.origin.y, webView.frame.size.width, webView.scrollView.contentSize.height);
}

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

Horizontal Scrolling in UIWebView Not Working

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.

Is there any similar solution to UIWebView?

I use a UIWebView in my app to present content, but now I can't do it anymore because I also need to use a UIScrollView and it creates conflicts related to scrolling.
Also, UIWebView is very slow.
So my question is : is there any another way to load string which contains HTML tags (strong, p, div, and etc...)?
UPDATE (improved explanation)
I have UIVIewController containing an image at the top, and under this image is a title and under the title is content (HTML string from web). The problem is that when the text is too big, the webview is scrolling, but not the whole page. I want the whole page to scroll, not just the webview.
You won't get anything faster or better than the native UIWebView. Probably you should overthink your UI/UX. What exactly do you want to achieve with a webview in a scrollview??
Perhaps attributed strings are enough for you. Look it up.
The OHAttributedLabel even parses HTML for you.
Solved.
-(void)webViewDidFinishLoad:(UIWebView *)webView {
NSString *output = [webView stringByEvaluatingJavaScriptFromString:#"document.body.scrollHeight;"];
int webViewHeight = [output intValue];
//NSLog(#"height: %d", webViewHeight);
if(webViewHeight > 220){
CGRect frame = articleContentWebView.frame;
frame.size.height = webViewHeight;
articleContentWebView.frame = frame;
int scrollViewheight = 416 + (webViewHeight - 220);
[articleScrollView setContentSize:CGSizeMake(320, scrollViewheight)];
}
}
In delegate method webViewDidFinishLoad I calculate height od UIWebView content and then set height of UIWebVIew and height of UIScrollView.

Text in UIWebView changes size or weight on device rotation

I have a UIWebView which at the moment displays a very simple HTML string. It looks fine in portrait but in landscape the text changes - the font looks heavier which is not what I want. See the image bellow:
The top line is what is shown in portrait and the bottom what is shown in landscape. The text is Chinese but the problem is the same with English.
Here is my code:
- (void)displayText
{
[self.myWebView loadHTMLString:#"<html style=\"-webkit-text-size-adjust: none; font-size-adjust:none; \"><head><meta name=\"viewport\" content=\"width=device-width; initial scale=1.0; maximum-scale=1.0;\"></head><body style=\"background-color:#FFFFFF; font-size:28px; color:#DDDDDD;\">你好。你好。你好。你好。你好。你好。你好。你好。</body></html> " baseURL:[NSURL URLWithString:#""]];
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
[self.myWebView reload];
}
Any ideas what I am doing wrong?
THANK YOU!
This CSS works for me:
-webkit-transform: translate3d(0,0,0);
I found the answer here:
UIWebView font is thinner in portrait than landscape
UPDATE:
One issue with the above CSS is there is some delay before text is rendered if the HTML is reloaded. I have found that setting the UIWebView's opaque property to FALSE works better:
self.myWebView.opaque = FALSE;
This seems like a bit of a hack. Hopefully there is a better solution.

Resources