Text
Widget for rendering text strings with font, color, and overflow control.
The guikit::Text class is a widget that renders a string of text using a Font and configurable color, size, and overflow behavior.
This doc assumes using namespace guikit is set and #include <guikit/text.hpp> is included.
Otherwise, use guikit::Text instead of Text.
Overflow
Controls how text behaves when it exceeds the available space.
enum class Overflow {
Wrap, // text wraps to the next line
Hidden // text is clipped to the widget bounds
};Constructor
Text(Font &font)
Creates a text widget that renders using the given font. The font must outlive the Text widget.
Text(Font &font);Example:
Font font;
font.loadFromFile("DejaVuSans.ttf");
Text label(font);Available methods
setString(const std::string &text)
Sets the text content to render.
void setString(const std::string &text);Example:
Text label(font);
label.setString("Hello, world!");setColor(const Color &color)
Sets the text color. Default is opaque white (255, 255, 255, 255).
void setColor(const Color &color);Example:
label.setColor(Color(255, 0, 0, 255)); // Red textsetFontSize(unsigned int size)
Sets the font size in pixels. Overrides the font's current size for this text widget.
void setFontSize(unsigned int size);Example:
label.setFontSize(24);setFont(Font &font)
Replaces the font used by this text widget.
void setFont(Font &font);Example:
Font boldFont;
boldFont.loadFromFile("DejaVuSans-Bold.ttf");
label.setFont(boldFont);setOverflow(Overflow mode)
Sets the overflow behavior. Use Overflow::Wrap to wrap text to new lines or Overflow::Hidden to clip excess text.
void setOverflow(Overflow mode);Example:
label.setOverflow(Overflow::Hidden);overflow()
Returns the current overflow mode.
Overflow overflow() const;Example:
if (label.overflow() == Overflow::Wrap) {
// text will wrap
}Overrides
render()
Renders the text widget. Called internally by the layout system.
void render() override;measure(float availableWidth, float availableHeight)
Computes the widget's desired size based on the text content and the available space. Called internally by the layout system.
void measure(float availableWidth, float availableHeight) override;Full Example
#include <guikit/text.hpp>
#include <guikit/font.hpp>
#include <guikit/window.hpp>
using namespace guikit;
int main() {
Window window("Text Demo", 800, 600);
Font font;
font.loadFromFile("/usr/share/fonts/truetype/DejaVuSans.ttf");
Text label(font);
label.setString("Hello gui-kit!");
label.setColor(Color(50, 200, 255, 255));
label.setFontSize(32);
// The widget system will call render() and measure()
// as part of the window layout loop.
window.show();
return 0;
}