Question

how to use variable i in place of '1.4'.i want to use variable i to zoom out and zoom in the webview.

[tp stringByEvaluatingJavaScriptFromString:@"document.body.style.zoom = '1.4';"];

Thanks in advance

Was it helpful?

Solution

You can use the NSString class method stringWithFormat:

[tp stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.body.style.zoom = '%f';", i];

OTHER TIPS

You can create a string using stringWithFormat:.

Something like [tp stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.body.style.zoom = '%d'", i];

To use stringWithFormat to create a string that you can pass to stringByEvaluatingJavaScriptFromString, you need to know the type of your scalar variable i.

If it's an int, use '%d'. If it's a float, use '%f'. If it's a double use '%lf'.

Or you could cast it to a certain type first:

[tp stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.body.style.zoom = '%lf';", (double)i];

[tp stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.body.style.zoom = '%f';", i]

%f stores i and says it is a float.

On a side note, if you want to specify how many decimals are displayed you would write %.02f

The zero tells it to pad the number with zeros. The '2' says show 2 decimal places. Another example: If you want three decimal places, use %.03f If you don't want the padding of zeros, remove the 0.

For 1.4, you would use %.1f

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top