0

I have an HTML file and JS file in my app. When the web view is loaded I am loading my html file which contains the reference to JavaScript file. I have added the JavaScript file in to my bundle resource for compile sources and in the web view did finish load I am calling a JavaScript function which increases the font size of HTML content but the JavaScript method is not getting called.

This is my code:

NSString *readerstring = @"document.getElementById('reader')";
      [webView stringByEvaluatingJavaScriptFromString:[[NSString alloc]initWithFormat:@"adjustFontSize('%@'.contentDocument, '4.0')",readerstring]];
1
  • There is no need to do [[NSString alloc] initWithFormat:] you can just do [NSString stringWithFormat:]. Commented May 24, 2013 at 9:17

2 Answers 2

1

You can change the font size of UIWebView like this,

int fontSize = 20;
NSString *String = [[NSString stringWithFormat:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '%d%%'", fontSize];
[myWebView stringByEvaluatingJavaScriptFromString:jsString];

or

[myWebView stringByEvaluatingJavaScriptFromString: @"document.body.style.fontSize = '8px'"];

Hope it will helps you...

Sign up to request clarification or add additional context in comments.

7 Comments

yes i know this but how to call .js file function in my webview
adjustFontSize() is a method in my js file
i am not much familier with js, please refer the following link stackoverflow.com/questions/14334047/… and stackoverflow.com/questions/8893852/…
refer this sample code for bridging between js and obj-c github.com/meadlai/MDCommonWebView
are you calling the js funtion after web view finished its loading?
|
0

Call the JS function after web view finished its loading.

And if you are using any of jQuery functions don forget to add jquery.js and evaluate it.

    -(void)webViewDidFinishLoad:(UIWebView *)webView{

    NSMutableString  *jsStr  =[[NSMutableString alloc] initWithData:[NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"jquery.min" ofType:@"js"]] encoding:NSUTF8StringEncoding];
    [WebView stringByEvaluatingJavaScriptFromString:jsStr];
jsStr  =[[NSMutableString alloc] initWithData:[NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"yourown" ofType:@"js"]] encoding:NSUTF8StringEncoding];
    [WebView stringByEvaluatingJavaScriptFromString:jsStr];
//evalute your js file before calling its function 
[WebView stringByEvaluatingJavaScriptFromString:@"myFunctionToChangethefontSize();"];

 //change font size directly
    [WebView stringByEvaluatingJavaScriptFromString:@"$('#divContent').css('font-size', '18px');"];

    }

Important:

Check whether the JS file in compile resource bundle then you have to remove it from there and add it in copy Bundle Resource.

8 Comments

did you evaluated your js file before calling your js function?
can i send u my html and js file
put alert(); inside that js function call at the beginning and at the end to check whether its called or not.
check whether the js file in compile resource bundle then you have to remove it from there and add it in copy Bundle Resourse
i have added alert in the js file but it not getting called
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.