Shiny Language Reference

Language Reference > Font Functions


getstringheight

int getstringheight(string Text, int Width);

 

 

Returns the height in pixels of the text constrained to a width

GETSTRINGHEIGHT returns the height in pixels of the specified text if it were to be drawn using the currently selected font (using selectfont) and constrained to the specified width per line.Because it relies on selectfont this function can only be used in the Render handler.

This statement performs a completely different operation to getfontheight, which returns the pixel height of the specified font at its specified point size. This is useful for calculating the height of one line of text. GETSTRINGHEIGHT returns the pixel height of a string of text when that text is rendered on multiple lines, where each line has a maximum width of the Width parameter. This is useful for formatting arbitrary strings.

The value returned by this statement is identical to the value returned by drawtext. However drawtext outputs the specified text to the display panel, whereas GETSTRINGHEIGHT performs the height calculation in memory without drawing anything.

This statement is only valid in the Render handler.

 

Parameters

Text

The text string to calculate the width of

Width

The maximum width in pixels of any one line of text.

 

Return Value

The height in pixels of the specified string if it were rendered in the current font, and constrained to a maximum line pixel length of Width.

 

Example Code

font MyFont;
createfont(MyFont, "Arial", 12);
selectfont(MyFont);

// Height will contain the height in pixels of the string where each line of
// the string is no more than 100 pixels wide

int Height = getstringheight("A text string", 100);