Legacy:Font/getStringWidth

From Spherical
< Legacy:Font
Revision as of 22:46, 24 June 2013 by Apollolux (talk | contribs) (filled)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Get the pixel width of a string if it were drawn with Font.drawText().

Usage

integer Font.getStringWidth(text);


  • text string. The text to measure the pixel width of

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);
  
  // clear the key queue
  while (AreKeysLeft())
    GetKey();
  
  // blit the text until a key is pressed
  while (!AreKeysLeft())
  {
    font.drawText(x, y, message);
    FlipScreen();
  }
}

See also

API:Font/navbox