comment utiliser des constantes variables au lieu
-
26-09-2019 - |
Question
comment utiliser i
variable à la place de '1.4'
.i veulent utiliser i
variable pour effectuer un zoom arrière et zoom avant WebView.
[tp stringByEvaluatingJavaScriptFromString:@"document.body.style.zoom = '1.4';"];
Merci à l'avance
La solution
Vous pouvez utiliser la méthode de classe NSString
stringWithFormat:
[tp stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.body.style.zoom = '%f';", i];
Autres conseils
Vous pouvez créer une chaîne en utilisant stringWithFormat:
.
Quelque chose comme [tp stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.body.style.zoom = '%d'", i];
Pour utiliser stringWithFormat pour créer une chaîne que vous pouvez passer à stringByEvaluatingJavaScriptFromString, vous devez connaître le type de votre variable scalaire i.
Si c'est un entier, utilisez '% d'. Si c'est un flotteur, utiliser « % f ». Si c'est une double utilisation% lf.
Ou vous pourriez jeter à un certain type d'abord:
[tp stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.body.style.zoom = '%lf';", (double)i];
[tp stringByEvaluatingJavaScriptFromString: [NSString stringWithFormat: @ "document.body.style.zoom = '% f';", i]
% f magasins i et dit qu'il est un flotteur.
Sur une note de côté, si vous souhaitez spécifier le nombre de décimales sont affichées, vous écririez % .02f
Le zéro, il dit à pavé le nombre avec des zéros. Le « 2 » dit montrer 2 décimales. Un autre exemple: Si vous voulez trois décimales, utilisez% .03f Si vous ne voulez pas le remplissage de zéros, enlever le 0.
Pour 1.4, utilisez% .1f