开发者

how to use variable instead of constants

开发者 https://www.devze.com 2023-01-14 10:54 出处:网络
how to use variable i in place of \'1.4\'.i want to use variable i to zoom o开发者_开发问答ut and zoom in the webview.

how to use variable i in place of '1.4'.i want to use variable i to zoom o开发者_开发问答ut and zoom in the webview.

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

Thanks in advance


You can use the NSString class method stringWithFormat:

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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消