How to change default hyper link colours in UIWebview - ios

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];
}

Related

Possible to show PDF over a PageViewController in an iOS app?

I am developing a mobile app in which I need to highlight PDF text after searching and I am using PDF kitten library for this.
Library link: https://github.com/KurtCode/PDFKitten
From this I got number of selections on the PDF as well as the content of the PDF but I am unable to highlight text as the PDF is loaded on the webview. Is this possible, to add my PDF document over pageview controller and highlight text over it.
First, create a new instance of the scanner.
CGPDFPageRef page = CGPDFDocumentGetPage(document, 1);
Scanner *scanner = [Scanner scannerWithPage:page];
Set a keyword (case-insensitive) and scan a page.
NSArray *selections = [scanner select:#"happiness"];
Finally, scan the page and draw the selections.
for (Selection *selection in selections)
{
// draw selection
}
and then highlight the selections using core graphics framework.

Formatting strings in UIWebView

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.

Dynamic font size in UIWebView

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];

White screen is displaying while loading local HTML files in Browser Field?

I am using BrowserField to display some local HTML files in my application. It is displaying the HTML files properly. But while starting of the screen it is displaying some white screen (background). How can i get rid of this issue?
I am using the below code:
BrowserFieldConfig _bfConfig = new BrowserFieldConfig();
_bfConfig.setProperty(BrowserFieldConfig.NAVIGATION_MODE,BrowserFieldConfig.NAVIGATION_MODE_POINTER);
_bfConfig.setProperty(BrowserFieldConfig.JAVASCRIPT_ENABLED, Boolean.TRUE );
_bfConfig.setProperty(BrowserFieldConfig.USER_AGENT, "MyApplication 1.0");
BrowserField myBrowserField = new BrowserField(_bfConfig);
add(myBrowserField);
BrowserFieldRequest request = new BrowserFieldRequest("local:///OTPhelp_en.html");
myBrowserField.requestContent(request);
I don't have a perfect answer for you. If you take a look at this question, so far, no answers have been given as to how to make the BrowserField background transparent, which would be one way to solve your problem.
Depending on how your OTPhelp_en.html page is written, how much control over it you have, and how often it changes, this may be a workaround that's acceptable:
If your html file has a solid background color, and you know what that color is (because it's your html content), then you could simply set the BrowserField background color to match. Then, you wouldn't see any white flash before the html content is rendered. Something like this:
public class MyBrowserScreen extends MainScreen {
// this assumes the html file uses a red (#ff0000) background
private int BG_COLOR = Color.RED;
public MyBrowserScreen() {
// set the screen manager's background
getMainManager().setBackground(BackgroundFactory.createSolidBackground(BG_COLOR));
BrowserFieldConfig _bfConfig = new BrowserFieldConfig();
_bfConfig.setProperty(BrowserFieldConfig.NAVIGATION_MODE,BrowserFieldConfig.NAVIGATION_MODE_POINTER);
_bfConfig.setProperty(BrowserFieldConfig.JAVASCRIPT_ENABLED, Boolean.TRUE );
_bfConfig.setProperty(BrowserFieldConfig.USER_AGENT, "MyApplication 1.0");
BrowserField myBrowserField = new BrowserField(_bfConfig);
// set the browser field background to match the HTML background, and
// the containing screen's background
_myBrowserField.setBackground(getMainManager().getBackground());
add(myBrowserField);
BrowserFieldRequest request = new BrowserFieldRequest("local:///OTPhelp_en.html");
myBrowserField.requestContent(request);
Of course, hardcoding it in this way means that if the HTML file changes its background color, you'll need to change it in the Java code, too.
If you wanted to avoid that, and you knew the HTML file would always use a solid background color, you could first open the html file as a resource stream
getClass().getResourceAsStream("/OTPhelp_en.html");
and then parse it, searching for the background color (e.g. <body bgcolor= or <body style="background-color:). That would at least allow the browser field to look right if a simple background color change is made in the HTML file.
If the HTML file uses a gradient background, or an image background, the above code will have to be changed. But, without more information, that's my suggestion for a workaround.

iOS: Web preferences for UIWebView?

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.

Resources