Legacy:Font/getStringWidth: Difference between revisions

From Spherical
Jump to navigation Jump to search
created from http://web.archive.org/web/20110906074311/http://www.spheredev.org/wiki/Font.getStringWidth
 
filled
 
Line 1: Line 1:
Get the pixel width of a string if it were drawn with [[API:Font/drawText|Font.drawText]]().
Get the pixel width of a string if it were drawn with [[API:Font/drawText|Font.drawText]]().


Line 8: Line 6:
{{Usage|returns=integer|object=Font|func=getStringWidth|params=text}}
{{Usage|returns=integer|object=Font|func=getStringWidth|params=text}}


* '''param1''' type. param1 description
* '''text''' string. The text to measure the pixel width of
* '''param2''' type. param2 description
* '''paramN''' type. paramN description


==Examples==
==Examples==
(examples with syntaxhighlighted code)
This will write a simple string that is centered on the screen.
 
<syntaxhighlight>
==Notes==
function game()
(notes)
{
  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();
  }
}
</syntaxhighlight>


==See also==
==See also==
* see also
* Sphere [[API:Font|Font]] object
* see also
* [[API:Font/drawText|Font.drawText]]()
* see also
* [[API:Font/getHeight|Font.getHeight]]()
* etc
* [[API:Font/getStringHeight|Font.getStringHeight]]()
* [[API:AreKeysLeft|AreKeysLeft]]()
* [[API:FlipScreen|FlipScreen]]()
* [[API:GetKey|GetKey]]()
* [[API:GetScreenHeight|GetScreenHeight]]()
* [[API:GetScreenWidth|GetScreenWidth]]()
* [[API:GetSystemFont|GetSystemFont]]()


{{API:Font/navbox}}
{{API:Font/navbox}}

Latest revision as of 22:46, 24 June 2013

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