gui-kit

API Reference

Text

Widget for rendering a string with a font, size, and color.

The guikit::Text class is a widget that renders a string of text using a Font with a configurable color and size. It measures its own size based on the rendered text.

This doc assumes using namespace guikit is set and #include <guikit/text.hpp> is included. Otherwise, use guikit::Text instead of Text.

Constructors

Text(std::string text)

Creates a text widget for the given string.

Text(std::string text);

Example:

Text label("Hello, world!");

Text(std::u8string text)

Creates a text widget from a UTF-8 string.

Text(std::u8string text);

Destructor

~Text()

Destroys the text widget and releases any rasterized text surface.

~Text();

Available methods

setFont(guikit::Font font)

Replaces the font used by this widget and re-measures the text.

Text &setFont(guikit::Font font);

Example:

Text label("Hello");
label.setFont(Font("/usr/share/fonts/truetype/DejaVuSans.ttf", 24));

setFontSize(float size)

Sets the font size in points and re-measures the text.

Text &setFontSize(float size);

Example:

label.setFontSize(32);

measureFont()

Measures the rendered string and applies the resulting width and height to the element. Called automatically by the constructor and by setFont / setFontSize.

void measureFont();

Overrides

render()

Rasterizes the text with the current font and color and draws it at the element's position. Called automatically by the render loop.

void render();

Full Example

#include <guikit/window.hpp>
#include <guikit/text.hpp>

using namespace guikit;

int main() {
    Window window("Text Demo", 800, 600);

    window.rootElement->addChild<Text>("Hello gui-kit!")
        .setFontSize(32)
        .setMarginTop(40)
        .setMarginLeft(40);

    window.show();
    return 0;
}

On this page