Skip to content

Text

Configure

cpp myScreen.selectFont(fontMax() - 1); myScreen.setFontSolid(false);

selectFont()
selects the font based on its index, from range 0..fontMax() - 1.
setFontSolid()
prints the text on a transparent background for false or on an opaque background for true.

Use

cpp linenums="1" myScreen.gText(10, 10, "Text", myColours.black, myColours.white);

gText()
prints a text at the graphic coordinates.

The required parameters are

  • The first line specifies the top-left coordinates x-y for the text.

The optional parameters are

  • The second and third lines are optional and specify the text and background colours.

Default values are black text on white background.

If the font is not solid, the background is not generated.

The GUI library offers more advanced control with the Text element.

cpp uint16_t sizeI = myScreen.characterSizeX('I'); uint16_t sizeM = myScreen.characterSizeX('M');

characterSizeX()
returns the size of the character on the x-axis.

As some fonts are proportional, a character is required as parameter.

characterSizeY()
returns the size of the font on the y-axis.

cpp uint16_t sizeI = myScreen.stringSizeX("Text"); uint16_t sizeM = myScreen.stringLengthToFitX("Text", 40);

stringSizeX()
returns the size of the string on the x-axis.
stringLengthToFitX()
returns the number of characters to fit a given number of pixels on the x-axis.

Example

This is the core of the code from example Common_Fonts.ino.

``` cpp void displayFonts() { uint16_t x = 10; uint16_t y = 10; myScreen.setOrientation(myOrientation);

myScreen.selectFont(fontLarge);

myScreen.gText(x, y, myScreen.WhoAmI(), myColours.red);
y += myScreen.characterSizeY();
myScreen.gText(x, y, formatString("%i x %i", myScreen.screenSizeX(), myScreen.screenSizeY()), myColours.red);

y += myScreen.characterSizeY();
y += myScreen.characterSizeY();

myScreen.selectFont(fontSmall);
myScreen.gText(x, y, "Terminal6x8");
y += myScreen.characterSizeY();

myScreen.selectFont(fontMedium);
myScreen.gText(x, y, "Terminal8x12");
y += myScreen.characterSizeY();

myScreen.selectFont(fontLarge);
myScreen.gText(x, y, "Terminal12x16");
y += myScreen.characterSizeY();

myScreen.selectFont(fontVery);
myScreen.gText(x, y, "Terminal16x24");
y += myScreen.characterSizeY();

// Extended
myScreen.selectFont(fontMedium);

if (STRING_MODE == USE_STRING_OBJECT)

String text = utf2iso("éàîüç $£€¥ ¿?");

elif (STRING_MODE == USE_CHAR_ARRAY)

char text[24];
strcpy(text, utf2iso("éàîüç $£€¥ ¿?"));

endif // STRING_MODE

uint16_t z = myScreen.stringSizeX(text);
uint16_t t = myScreen.characterSizeY();
myScreen.gText(myScreen.screenSizeX() - z, myScreen.screenSizeY() - t, text);
y += myScreen.characterSizeY();

myScreen.flush();

} ```