Skip to content

Text

Configure

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

1
2
3
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 controls on text, with Label, Text and TextBox.

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.

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_Text.ino.

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

    // Add fonts
    uint8_t fontMono, fontSans, fontSerif;

#if (FONT_MODE == USE_FONT_TERMINAL)

#elif (FONT_MODE == USE_FONT_HEADER)

    fontMono = myScreen.addFont(Font_DejaVuMono20);
    fontSans = myScreen.addFont(Font_DejaVuSans20);
    fontSerif = myScreen.addFont(Font_DejaVuSerif20);

    fontMono -= (fontMono > 0) ? 1 : 0;
    fontSans -= (fontSans > 0) ? 1 : 0;
    fontSerif -= (fontSerif > 0) ? 1 : 0;

    hV_HAL_Serial_printf("%10s= %i", "fontMono", fontMono);
    hV_HAL_Serial_crlf();
    hV_HAL_Serial_printf("%10s= %i", "fontSans", fontSans);
    hV_HAL_Serial_crlf();
    hV_HAL_Serial_printf("%10s= %i", "fontSerif", fontSerif);
    hV_HAL_Serial_crlf();

#elif (FONT_MODE == USE_FONT_FLASH)

    fontMono = Font_DejaVuMono20;
    fontSans = Font_DejaVuSans20;
    fontSerif = Font_DejaVuSerif20;

#else
#error FONT_MODE not defined
#endif

    myScreen.selectFont(fontSans);

    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(fontMono);
    myScreen.gText(x, y, "fontMono");
    y += myScreen.characterSizeY();

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

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

#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();
}