On Mac OS/X, you can programmatically set the default font and many other values in a WebView by settings its web preferences.
Thanks.
To set the font you could just use CSS and JavaScript. Do the following in the UIWebView page loaded delegate event to set the default font size on any page:
NSInteger fontSizePercent = 150;
NSString *script = [NSString stringWithFormat:#"document.getElementsByTagName('head')[0].innerHTML += '<style>body {font-size: %i%% !important}</style>'", fontSizePercent];
[webView stringByEvaluatingJavaScriptFromString:script];
Only problem is that you may get a flash of the normal font size briefly before your script is run.
Related
I am generating pdf from word document using UIPrintPageRenderer. for this, I am loading doc file in UIwebview and on WebviewdidFinishLoad method, I am generating pdf usingUIPrintPageRenderer. this part is working fine.
Next, I need to detect if generated pdf file is Monochrome or coloured, to decide print price within app.
Problem is,UIWebview is displaying all Hyperlinks in words docs in blue fonts by default and hence generated pdf become coloured. I want to render hyperlinks with Black color as default.
I have tried following but not working:
1) set tint color of UIWebview.
webview.tintColor = [UIColor blackColor];
2) Styling hyper link in uiweview with javascript
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
NSString *javaScriptCodeToExecute = #"var link = window.document.getElementById('urlId'); link.style['text-decoration'] = 'none'; link.style.color = 'black';";
[_webView stringByEvaluatingJavaScriptFromString:javaScriptCodeToExecute];
}
I'm building an iOS app which supports iOS 9 and above.
I'm using a UIWebView to display text stored in RTF documents which I am including in my app's bundle. I use the following code to insert the contents of the RTF files into the web view:
NSURL *filePath = [[NSBundle mainBundle] URLForResource:self.detailItem withExtension:#"rtf"];
NSURLRequest *request = [NSURLRequest requestWithURL:filePath];
[self.detailWebView loadRequest:request];
I am also using this to format the text to the correct size:
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
NSString *fontSize = #"80";
NSString *jsString = [[NSString alloc] initWithFormat:#"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '%d%%'", [fontSize intValue]];
[self.detailWebView stringByEvaluatingJavaScriptFromString:jsString];
}
I never usually use JavaScript, so I'm not claiming to know how that works, but it enables me to adjust the fontSize string and achieve exactly the size that I want. The text displays in the same font as the rest of the stings in my app and everything looks fine.
The problem is that I want to be able to display some other text in the web view but I want to load it from an NSString in code, so I can append other strings and manipulate what the user sees. When I do this instead of loading from the RTF file, I get very different formatting:
[self.detailWebView loadHTMLString:#"This is a string" baseURL:nil];
This comes out in some nasty Times New Roman style font, and the text way smaller than the text that is loaded from the RTF files. I realise I can build some html tags into my string to add formatting, but I want to understand why the js formatting in webViewDidFinishLoad is not being applied, and what I can do to achieve universal formatting across all the strings I use in my web view.
If you need some UIWebView text formatting on iOS, you can look at my approach with SWIFT. https://github.com/Vanyaslav/Swiftyyjsformatter
Hope, it helps.
I have in my application a UIWebView that is it used often to display informatin to the user.
I have a requirement that is that the user should be able to change the font size of that web view to display the text bigger or smaller.
What I've done is to hook up two buttons in my ViewController that calls a JS script in my web view that changes the HTML body font size.
It is working nice, but if the user changes in the iPhone Settings the text size it doesn't affect my web view fonts.
Is it possible to use something like the methods preferredFontForTextStyle in a UIWebView natively?
NSString *jsForTextSize = [[NSString alloc] initWithFormat:#"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '%f%%'", changeFontSize*100/defaultFontSize];
[self.webView stringByEvaluatingJavaScriptFromString:jsForTextSize];
I am trying to use the very same font in a UIWebView on iOS that the iOS system uses currently.
This means I want to use the very same font I'd get from:
UIFontDescriptor *fd;
fd = [UIFontDescriptor preferredFontDescriptorWithTextStyle:UIFontTextStyleBody];
UIFont *font = [UIFont fontWithDescriptor:fd size:0];
There are lots of answers on SO (iPhone Development - Setting UIWebView font, Using custom font in a UIWebView, How to change UIWebView default font, How to set custom font in UIWebView?) that explain how to embed a UIFont's familyName or fontName into an html's style attributes, by doing this:
NSString *fontStyle, *htmlString = #"test";
fontStyle = [NSString stringWithFormat:#"font-family:%#;font-size:%d", font.familyName, (int)font.pointSize];
htmlString = NSString stringWithFormat:#"<span style=\"%#\">%#</span>", fontStyle, htmlString];
But this doesn't seem to work on iOS 7 and 8 any more where these system font names are now like:
font.fontName: .HelveticaNeueInterface-Regular
font.familyName: .Helvetica Neue Interface
When using these names in an UIWebView with the "font-family" attribute, the text gets rendered in the default font (which is with serifs and certainly not the new iOS system font). Removing the leading period doesn't help, either.
So, how do I now translate the system's font into a name that gets correctly interpreted by the Web Kit?
Please understand that I am seeking for a generic solution and do not simply seek the iOS 7/8 name I can insert manually into the html style attribute.
I just ran into this blog article which may offer the solution though I have not verified it yet:
If you're trying to reference the system font in web views just prepend -apple-system to your font-family list and it'll use SF in iOS 9 and Helvetica in older versions.
Which means that the font line of above code needs to be change to:
fontStyle = [NSString stringWithFormat:#"font-family:-apple-system,Helvetica Neue;font-size:%d", (int)font.pointSize];
That should take care of selecting the system font from iOS 7 onward.
what about this article: http://furbo.org/2015/07/09/i-left-my-system-fonts-in-san-francisco/ from Craig Hockenberry?
Have you tried Helvetica or San Francisco with attaching font files?
In my application I am loading HTML String which is parsed from the xml file I need to change the color, name and size of the font. To replace that I have already use the followings code but its not working
NSString *webString = [[NSString alloc] initWithFormat:#"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '%d%%'",
textFontSize];
[web stringByEvaluatingJavaScriptFromString:webString];
Is there any way then please suggest me the answer.
Thanks in Advanced!!!
Use NSAttributedString instead. Here's the docs from Apple. You can set the font type, colour, and size. You can then convert NSAttributedString back to HTML this way.
It's only available on iOS 6 and above.