Font.drawTextBox
From Spheriki
Draws text in a word-wrapped block.
Contents |
[edit]
Usage
Font.drawTextBox(x, y, w, h, y_offset, text);
- Font Sphere Font object. The font to draw the text box with.
- x number. Dimensions of the box, in pixels. Text will not appear outside this box.
- y number. See above.
- w number, positive. Width, see above.
- h number, positive. Height, see above.
- y_offset number. The Y coordinate of the text within the box. Positive numbers will push the text down within the box, negative numbers will scroll it upwards. See notes.
- text string. The text to ultimately draw.
[edit]
Examples
To simply draw a block of wrapped text, use something like this:
var font = GetSystemFont(); font.drawTextBox( 5, 5, 100, 30, 0, "Bob: I can talk this this all day. Yak yak yak yak yak yak." ); FlipScreen(); GetKey();
We can use escape codes to control text output (\n for newline, \t for tab):
var font = GetSystemFont(); font.drawTextBox( 5, 5, 200, 30, 0, "Bob:\n\tI am saying stuff right here!\nHahaha!" ); FlipScreen(); GetKey();
This will show:
Bob:
I am saying stuff right here!
Hahaha!
Here's a simple script to show some upwards scrolling text. It finds the text block height, and uses the Y offset parameter to achieve the scrolling effect:
function ScrollText(font, x, y, w, h, text)
{
var text_height = font.getStringHeight(text, w);
var scroll_y = text_height;
while (scroll_y > -text_height && !IsAnyKeyPressed())
{
font.drawTextBox(
x, y, w, h,
Math.floor(scroll_y),
text );
FlipScreen();
scroll_y -= 0.5;
}
}
function game()
{
var my_text = "Come up with your own text, " +
"I'm too damn tired to come up with anything. " +
"Anything original, anyway.\n" +
"\n" +
"\tGood night.";
var font = GetSystemFont();
var x = Math.floor(GetScreenWidth() / 4);
var y = Math.floor(GetScreenHeight() / 4);
var width = x * 2;
var height = y * 2;
SetFrameRate(40);
ScrollText(GetSystemFont(), x, y, width, height, my_text);
}
[edit]
Notes
- The y_offset parameter only moves the text inside to the box, and not the box itself. Positive numbers will shift it downwards, and negative numbers pull it up. If the displayed text would be shown outside the box (x, y, w, h), then it will be clipped. The last example above demonstrates this.
- Unlike Font.drawText(), this function supports what's known as escape codes. These allow you to control the text:
- \n will insert a new line. The text after this will begin on a new line.
- \t will insert a tab. This is currently the same as inserting 8 space characters.
- If you are revealing text character-by-character (common in RPGs), sometimes words will appear on one line before being broken onto the next at its full width. The only way to get around this is to wrap the text yourself. Search the Scripts category for scripts that already do this.
[edit]
See also
- Sphere Font object

