Font.getStringWidth
From Spheriki
Get the pixel width of a string if it were drawn with Font.drawText().
[edit]
Usage
number font.getStringWidth(text);
- font Sphere Font object. The font to gauge the string width with.
- text String. The text to measure the pixel width of.
- number is returned, which is the the number of pixels wide the string would be if drawn with the font.
[edit]
Examples
This will write a simple string that is centered on the screen.
function game()
{
var message = "Hi everybody! Press a key to end.";
var font = GetSystemFont();
var height = font.getHeight();
var width = font.getStringWidth(message);
var x = GetScreenWidth() - Math.floor(width / 2);
var y = GetScreenHeight() - Math.floor(height / 2);
while (AreKeysLeft())
GetKey();
while (!AreKeysLeft())
{
font.drawText(x, y, message);
FlipScreen();
}
}
[edit]
See also
- Sphere Font object

