Skip to content

Labels and texts

Juuz edited this page Aug 2, 2024 · 3 revisions

Labels are the simplest widget. They display a single line of text. There are two types of labels in LibGui: WLabels and WDynamicLabels.

WLabel has a normal Text object which can be manipulated with setters. WDynamicLabel pulls its text as a string each time it renders, so it's useful for rapidly updating data.

A WLabel can be made from any kind of Text, like Text.translatable or Text.literal.

Example:

WLabel label = new WLabel(Text.translatable("block.minecraft.stone"));
root.add(label, 0, 0, 4, 1);

A simple label

Aligning labels

The text of a label is on the left side by default. You can use WLabel.setHorizontalAlignment() to change the alignment to CENTER or RIGHT.

label.setHorizontalAlignment(HorizontalAlignment.CENTER);

A centered label

Colors

The color of a label changes depending on the darkmode setting of LibGui. You can tweak your labels light and dark colors using WLabel.setColor() and WLabel.setDarkmodeColor(). The darkmode color can be disabled using WLabel.disableDarkmode().

label.setColor(0xFF0000).setDarkmodeColor(0x0000FF);

A colored label

Dynamic labels

Dynamic labels (WDynamicLabel) gets its text from a Supplier<String> each time it renders.

Translations in dynamic labels should be done with Minecraft's I18n class.

// A label that shows the time:
WDynamicLabel label = new WDynamicLabel(() -> I18n.translate("text.my_mod.time", System.currentTimeMillis()));

en_us.json:

{
  "text.my_mod.time": "Time: %d"
}

A dynamic label