Legacy:Font/getStringWidth: Difference between revisions
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}} | ||
* ''' | * '''text''' string. The text to measure the pixel width of | ||
==Examples== | ==Examples== | ||
( | This will write a simple string that is centered on the screen. | ||
<syntaxhighlight> | |||
== | 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(); | |||
} | |||
} | |||
</syntaxhighlight> | |||
==See also== | ==See also== | ||
* | * Sphere [[API:Font|Font]] object | ||
* | * [[API:Font/drawText|Font.drawText]]() | ||
* | * [[API:Font/getHeight|Font.getHeight]]() | ||
* | * [[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
- Sphere Font object
- Font.drawText()
- Font.getHeight()
- Font.getStringHeight()
- AreKeysLeft()
- FlipScreen()
- GetKey()
- GetScreenHeight()
- GetScreenWidth()
- GetSystemFont()