views:

66

answers:

4

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

+2  A: 

You can use the NSString class method stringWithFormat:

[tp stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.body.style.zoom = '%f';", i];
Jacob Relkin
If you use a `d` specifier, you'll lose the decimal point.
Shaggy Frog
Right. I changed it to `%f`.
Jacob Relkin
%f could well do something completely bogus, depending on the type used to declare i (for instance, as a double).
hotpaw2
A: 

You can create a string using stringWithFormat:.

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

Aloha Silver
I was writing as you posted, Jacob. Sorry about that.
Aloha Silver
I also didn't notice it was a float. It should be %f instead of %d.
Aloha Silver
A: 

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];
hotpaw2
A: 

[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

OOProg