Pregunta

cómo utilizar i variable en lugar de '1.4'.i quieren utilizar i variable para alejar y acercar la vista web.

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

Gracias de antemano

¿Fue útil?

Solución

Puede utilizar el método de la clase NSString stringWithFormat:

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

Otros consejos

Puede crear una cadena usando stringWithFormat:.

Algo así como [tp stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.body.style.zoom = '%d'", i];

Para usar stringWithFormat para crear una cadena que se puede pasar a stringByEvaluatingJavaScriptFromString, es necesario conocer el tipo de la variable escalar i.

Si se trata de un int, utilice '% d'. Si se trata de un flotador, utilizar '% f'. Si se trata de un doble uso '% lf'.

O bien, podría echarlo a un cierto tipo primero:

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

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

f% i tiendas y dice que es un flotador.

En una nota lateral, si desea especificar el número de decimales se muestran escribiría % .02f

El cero indica que debe rellenar el número de ceros. El '2' dice mostrar 2 decimales. Otro ejemplo: Si quieres tres cifras decimales, utilice% .03f Si no desea que el relleno de ceros, quitar el 0.

En 1.4, que usaría% .1f

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top