gtk.label

Module for [Label] class

Types 3

The [gtk.label.Label] widget displays a small amount of text.

As the name implies, most labels are used to label another widget such as a class@Button.

!An example GtkLabel

CSS nodes

label
├── [selection]
├── [link]
┊
╰── [link]

[gtk.label.Label] has a single CSS node with the name label. A wide variety of style classes may be applied to labels, such as .title, .subtitle, .dim-label, etc. In the [gtk.shortcuts_window.ShortcutsWindow], labels are used with the .keycap style class.

If the label has a selection, it gets a subnode with name selection.

If the label has links, there is one subnode per link. These subnodes carry the link or visited state depending on whether they have been visited. In this case, label node also gets a .link style class.

GtkLabel as GtkBuildable

The GtkLabel implementation of the GtkBuildable interface supports a custom <attributes> element, which supports any number of <attribute> elements. The <attribute> element has attributes named “name“, “value“, “start“ and “end“ and allows you to specify [pango.attribute.Attribute] values for this label.

An example of a UI definition fragment specifying Pango attributes:

<object class="GtkLabel">
 <attributes>
   <attribute name="weight" value="PANGO_WEIGHT_BOLD"/>
   <attribute name="background" value="red" start="5" end="10"/>
 </attributes>
</object>

The start and end attributes specify the range of characters to which the Pango attribute applies. If start and end are not specified, the attribute is applied to the whole text. Note that specifying ranges does not make much sense with translatable attributes. Use markup embedded in the translatable content instead.

Accessibility

[gtk.label.Label] uses the [gtk.types.AccessibleRole.Label] role.

Mnemonics

Labels may contain “mnemonics”. Mnemonics are underlined characters in the label, used for keyboard navigation. Mnemonics are created by providing a string with an underscore before the mnemonic character, such as "_File", to the functions [gtk.label.Label.newWithMnemonic] or [gtk.label.Label.setTextWithMnemonic].

Mnemonics automatically activate any activatable widget the label is inside, such as a [gtk.button.Button]; if the label is not inside the mnemonic’s target widget, you have to tell the label about the target using [gtk.label.Label.setMnemonicWidget].

Here’s a simple example where the label is inside a button:

// Pressing Alt+H will activate this button
GtkWidget *button = gtk_button_new ();
GtkWidget *label = gtk_label_new_with_mnemonic ("_Hello");
gtk_button_set_child (GTK_BUTTON (button), label);

There’s a convenience function to create buttons with a mnemonic label already inside:

// Pressing Alt+H will activate this button
GtkWidget *button = gtk_button_new_with_mnemonic ("_Hello");

To create a mnemonic for a widget alongside the label, such as a [gtk.entry.Entry], you have to point the label at the entry with [gtk.label.Label.setMnemonicWidget]:

// Pressing Alt+H will focus the entry
GtkWidget *entry = gtk_entry_new ();
GtkWidget *label = gtk_label_new_with_mnemonic ("_Hello");
gtk_label_set_mnemonic_widget (GTK_LABEL (label), entry);

Markup (styled text)

To make it easy to format text in a label (changing colors, fonts, etc.), label text can be provided in a simple markup format:

Here’s how to create a label with a small font:

GtkWidget *label = gtk_label_new (NULL);
gtk_label_set_markup (GTK_LABEL (label), "<small>Small text</small>");

(See the Pango manual for complete documentation] of available tags, func@Pango.parse_markup)

The markup passed to [gtk.label.Label.setMarkup] must be valid; for example, literal `<`, `>` and `&` characters must be escaped as <, >, and &. If you pass text obtained from the user, file, or a network to [gtk.label.Label.setMarkup], you’ll want to escape it with func@GLib.markup_escape_text or func@GLib.markup_printf_escaped.

Markup strings are just a convenient way to set the [pango.attr_list.AttrList] on a label; [gtk.label.Label.setAttributes] may be a simpler way to set attributes in some cases. Be careful though; [pango.attr_list.AttrList] tends to cause internationalization problems, unless you’re applying attributes to the entire string (i.e. unless you set the range of each attribute to [0, G_MAXINT)). The reason is that specifying the start_index and end_index for a [pango.attribute.Attribute] requires knowledge of the exact string being displayed, so translations will cause problems.

Selectable labels

Labels can be made selectable with [gtk.label.Label.setSelectable]. Selectable labels allow the user to copy the label contents to the clipboard. Only labels that contain useful-to-copy information—such as error messages—should be made selectable.

Text layout

A label can contain any number of paragraphs, but will have performance problems if it contains more than a small number. Paragraphs are separated by newlines or other paragraph separators understood by Pango.

Labels can automatically wrap text if you call [gtk.label.Label.setWrap].

[gtk.label.Label.setJustify] sets how the lines in a label align with one another. If you want to set how the label as a whole aligns in its available space, see the [gtk.widget.Widget.halign] and [gtk.widget.Widget.valign] properties.

The property@Gtk.Label:width-chars and property@Gtk.Label:max-width-chars properties can be used to control the size allocation of ellipsized or wrapped labels. For ellipsizing labels, if either is specified (and less than the actual text size), it is used as the minimum width, and the actual text size is used as the natural width of the label. For wrapping labels, width-chars is used as the minimum width, if specified, and max-width-chars is used as the natural width. Even if max-width-chars specified, wrapping labels will be rewrapped to use all of the available width.

Links

GTK supports markup for clickable hyperlinks in addition to regular Pango markup. The markup for links is borrowed from HTML, using the <a> with “href“, “title“ and “class“ attributes. GTK renders links similar to the way they appear in web browsers, with colored, underlined text. The “title“ attribute is displayed as a tooltip on the link. The “class“ attribute is used as style class on the CSS node for the link.

An example of inline links looks like this:

const char *text =
"Go to the "
"<a href=\"https://www.gtk.org\" title=\"<i>Our</i> website\">"
"GTK website</a> for more...";
GtkWidget *label = gtk_label_new (NULL);
gtk_label_set_markup (GTK_LABEL (label), text);

It is possible to implement custom handling for links and their tooltips with the signal@Gtk.Label::activate-link signal and the [gtk.label.Label.getCurrentUri] function.

Methods
GType _gType() @property
Label self()Returns `this`, for use in `with` statements.
LabelGidBuilder builder()Get builder for [gtk.label.Label] Returns: New builder object
pango.attr_list.AttrList attributes() @propertyGet `attributes` property. Returns: A list of style attributes to apply to the text of the label.
void attributes(pango.attr_list.AttrList propval) @propertySet `attributes` property. Params: propval = A list of style attributes to apply to the text of the label.
pango.types.EllipsizeMode ellipsize() @propertyGet `ellipsize` property. Returns: The preferred place to ellipsize the string, if the label does not have enough room to display the entire string.
void ellipsize(pango.types.EllipsizeMode propval) @propertySet `ellipsize` property. Params: propval = The preferred place to ellipsize the string, if the label does not have enough room to display the entire string.
gio.menu_model.MenuModel extraMenu() @propertyGet `extraMenu` property. Returns: A menu model whose contents will be appended to the context menu.
void extraMenu(gio.menu_model.MenuModel propval) @propertySet `extraMenu` property. Params: propval = A menu model whose contents will be appended to the context menu.
gtk.types.Justification justify() @propertyGet `justify` property. Returns: The alignment of the lines in the text of the label, relative to each other.
void justify(gtk.types.Justification propval) @propertySet `justify` property. Params: propval = The alignment of the lines in the text of the label, relative to each other.
string label() @propertyGet `label` property. Returns: The contents of the label.
void label(string propval) @propertySet `label` property. Params: propval = The contents of the label.
int lines() @propertyGet `lines` property. Returns: The number of lines to which an ellipsized, wrapping label should be limited.
void lines(int propval) @propertySet `lines` property. Params: propval = The number of lines to which an ellipsized, wrapping label should be limited.
int maxWidthChars() @propertyGet `maxWidthChars` property. Returns: The desired maximum width of the label, in characters.
void maxWidthChars(int propval) @propertySet `maxWidthChars` property. Params: propval = The desired maximum width of the label, in characters.
uint mnemonicKeyval() @propertyGet `mnemonicKeyval` property. Returns: The mnemonic accelerator key for the label.
gtk.widget.Widget mnemonicWidget() @propertyGet `mnemonicWidget` property. Returns: The widget to be activated when the labels mnemonic key is pressed.
void mnemonicWidget(gtk.widget.Widget propval) @propertySet `mnemonicWidget` property. Params: propval = The widget to be activated when the labels mnemonic key is pressed.
gtk.types.NaturalWrapMode naturalWrapMode() @propertyGet `naturalWrapMode` property. Returns: Select the line wrapping for the natural size request.
void naturalWrapMode(gtk.types.NaturalWrapMode propval) @propertySet `naturalWrapMode` property. Params: propval = Select the line wrapping for the natural size request.
bool selectable() @propertyGet `selectable` property. Returns: Whether the label text can be selected with the mouse.
void selectable(bool propval) @propertySet `selectable` property. Params: propval = Whether the label text can be selected with the mouse.
bool singleLineMode() @propertyGet `singleLineMode` property. Returns: Whether the label is in single line mode.
void singleLineMode(bool propval) @propertySet `singleLineMode` property. Params: propval = Whether the label is in single line mode.
pango.tab_array.TabArray tabs() @propertyGet `tabs` property. Returns: Custom tabs for this label.
void tabs(pango.tab_array.TabArray propval) @propertySet `tabs` property. Params: propval = Custom tabs for this label.
bool useMarkup() @propertyGet `useMarkup` property. Returns: true if the text of the label includes Pango markup.
void useMarkup(bool propval) @propertySet `useMarkup` property. Params: propval = true if the text of the label includes Pango markup.
bool useUnderline() @propertyGet `useUnderline` property. Returns: true if the text of the label indicates a mnemonic with an _ before the mnemonic character.
void useUnderline(bool propval) @propertySet `useUnderline` property. Params: propval = true if the text of the label indicates a mnemonic with an _ before the mnemonic character.
int widthChars() @propertyGet `widthChars` property. Returns: The desired width of the label, in characters.
void widthChars(int propval) @propertySet `widthChars` property. Params: propval = The desired width of the label, in characters.
bool wrap() @propertyGet `wrap` property. Returns: true if the label text will wrap if it gets too wide.
void wrap(bool propval) @propertySet `wrap` property. Params: propval = true if the label text will wrap if it gets too wide.
pango.types.WrapMode wrapMode() @propertyGet `wrapMode` property. Returns: Controls how the line wrapping is done.
void wrapMode(pango.types.WrapMode propval) @propertySet `wrapMode` property. Params: propval = Controls how the line wrapping is done.
float xalign() @propertyGet `xalign` property. Returns: The horizontal alignment of the label text inside its size allocation.
void xalign(float propval) @propertySet `xalign` property. Params: propval = The horizontal alignment of the label text inside its size allocation.
float yalign() @propertyGet `yalign` property. Returns: The vertical alignment of the label text inside its size allocation.
void yalign(float propval) @propertySet `yalign` property. Params: propval = The vertical alignment of the label text inside its size allocation.
gtk.label.Label newWithMnemonic(string str = null)Creates a new [gtk.label.Label], containing the text in str.
pango.attr_list.AttrList getAttributes()Gets the label's attribute list.
string getCurrentUri()Returns the URI for the currently active link in the label.
pango.types.EllipsizeMode getEllipsize()Returns the ellipsizing position of the label.
gio.menu_model.MenuModel getExtraMenu()Gets the extra menu model of label.
gtk.types.Justification getJustify()Returns the justification of the label.
string getLabel()Fetches the text from a label.
pango.layout.Layout getLayout()Gets the [pango.layout.Layout] used to display the label.
void getLayoutOffsets(out int x, out int y)Obtains the coordinates where the label will draw its [pango.layout.Layout].
int getLines()Gets the number of lines to which an ellipsized, wrapping label should be limited.
int getMaxWidthChars()Retrieves the desired maximum width of label, in characters.
uint getMnemonicKeyval()Return the mnemonic accelerator.
gtk.widget.Widget getMnemonicWidget()Retrieves the target of the mnemonic (keyboard shortcut) of this label.
gtk.types.NaturalWrapMode getNaturalWrapMode()Returns line wrap mode used by the label.
bool getSelectable()Returns whether the label is selectable. Returns: true if the user can copy text from the label
bool getSelectionBounds(out int start, out int end)Gets the selected range of characters in the label.
bool getSingleLineMode()Returns whether the label is in single line mode. Returns: true when the label is in single line mode.
pango.tab_array.TabArray getTabs()Gets the tabs for self.
string getText()Fetches the text from a label.
bool getUseMarkup()Returns whether the label’s text is interpreted as Pango markup.
bool getUseUnderline()Returns whether an embedded underlines in the label indicate mnemonics.
int getWidthChars()Retrieves the desired width of label, in characters.
bool getWrap()Returns whether lines in the label are automatically wrapped.
pango.types.WrapMode getWrapMode()Returns line wrap mode used by the label.
float getXalign()Gets the `xalign` of the label.
float getYalign()Gets the `yalign` of the label.
void selectRegion(int startOffset, int endOffset)Selects a range of characters in the label, if the label is selectable.
void setAttributes(pango.attr_list.AttrList attrs = null)Apply attributes to the label text.
void setEllipsize(pango.types.EllipsizeMode mode)Sets the mode used to ellipsize the text.
void setExtraMenu(gio.menu_model.MenuModel model = null)Sets a menu model to add when constructing the context menu for label.
void setJustify(gtk.types.Justification jtype)Sets the alignment of the lines in the text of the label relative to each other.
void setLabel(string str)Sets the text of the label.
void setLines(int lines)Sets the number of lines to which an ellipsized, wrapping label should be limited.
void setMarkup(string str)Sets the labels text and attributes from markup.
void setMarkupWithMnemonic(string str)Sets the labels text, attributes and mnemonic from markup.
void setMaxWidthChars(int nChars)Sets the desired maximum width in characters of label to n_chars.
void setMnemonicWidget(gtk.widget.Widget widget = null)Associate the label with its mnemonic target.
void setNaturalWrapMode(gtk.types.NaturalWrapMode wrapMode)Select the line wrapping for the natural size request.
void setSelectable(bool setting)Makes text in the label selectable.
void setSingleLineMode(bool singleLineMode)Sets whether the label is in single line mode.
void setTabs(pango.tab_array.TabArray tabs = null)Sets the default tab stops for paragraphs in self.
void setText(string str)Sets the text within the [gtk.label.Label] widget.
void setTextWithMnemonic(string str)Sets the label’s text from the string str.
void setUseMarkup(bool setting)Sets whether the text of the label contains markup.
void setUseUnderline(bool setting)Sets whether underlines in the text indicate mnemonics.
void setWidthChars(int nChars)Sets the desired width in characters of label to n_chars.
void setWrap(bool wrap)Toggles line wrapping within the [gtk.label.Label] widget.
void setWrapMode(pango.types.WrapMode wrapMode)Controls how line wrapping is done.
void setXalign(float xalign)Sets the `xalign` of the label.
void setYalign(float yalign)Sets the `yalign` of the label.
gulong connectActivateCurrentLink(T)(T callback, Flag!"After" after = No.After) if (isCallable!T && is(ReturnType!T == void) && (Parameters!T.length < 1 || (ParameterStorageClassTuple!T[0] == ParameterStorageClass.none && is(Parameters!T[0] : gtk.label.Label))) && Parameters!T.length < 2)Connect to `ActivateCurrentLink` signal.
gulong connectActivateLink(T)(T callback, Flag!"After" after = No.After) if (isCallable!T && is(ReturnType!T == bool) && (Parameters!T.length < 1 || (ParameterStorageClassTuple!T[0] == ParameterStorageClass.none && is(Parameters!T[0] == string))) && (Parameters!T.length < 2 || (ParameterStorageClassTuple!T[1] == ParameterStorageClass.none && is(Parameters!T[1] : gtk.label.Label))) && Parameters!T.length < 3)Connect to `ActivateLink` signal.
gulong connectCopyClipboard(T)(T callback, Flag!"After" after = No.After) if (isCallable!T && is(ReturnType!T == void) && (Parameters!T.length < 1 || (ParameterStorageClassTuple!T[0] == ParameterStorageClass.none && is(Parameters!T[0] : gtk.label.Label))) && Parameters!T.length < 2)Connect to `CopyClipboard` signal.
gulong connectMoveCursor(T)(T callback, Flag!"After" after = No.After) if (isCallable!T && is(ReturnType!T == void) && (Parameters!T.length < 1 || (ParameterStorageClassTuple!T[0] == ParameterStorageClass.none && is(Parameters!T[0] == gtk.types.MovementStep))) && (Parameters!T.length < 2 || (ParameterStorageClassTuple!T[1] == ParameterStorageClass.none && is(Parameters!T[1] == int))) && (Parameters!T.length < 3 || (ParameterStorageClassTuple!T[2] == ParameterStorageClass.none && is(Parameters!T[2] == bool))) && (Parameters!T.length < 4 || (ParameterStorageClassTuple!T[3] == ParameterStorageClass.none && is(Parameters!T[3] : gtk.label.Label))) && Parameters!T.length < 5)Connect to `MoveCursor` signal.
Constructors
this(void * ptr, Flag!"Take" take)
this(string str = null)Creates a new label with the given text inside it.
Methods
T attributes(pango.attr_list.AttrList propval)Set `attributes` property. Params: propval = A list of style attributes to apply to the text of the label. Returns: Builder instance for fluent chaining
T ellipsize(pango.types.EllipsizeMode propval)Set `ellipsize` property. Params: propval = The preferred place to ellipsize the string, if the label does not have enough room to display the entire string.
T extraMenu(gio.menu_model.MenuModel propval)Set `extraMenu` property. Params: propval = A menu model whose contents will be appended to the context menu. Returns: Builder instance for fluent chaining
T justify(gtk.types.Justification propval)Set `justify` property. Params: propval = The alignment of the lines in the text of the label, relative to each other.
T label(string propval)Set `label` property. Params: propval = The contents of the label.
T lines(int propval)Set `lines` property. Params: propval = The number of lines to which an ellipsized, wrapping label should be limited.
T maxWidthChars(int propval)Set `maxWidthChars` property. Params: propval = The desired maximum width of the label, in characters.
T mnemonicWidget(gtk.widget.Widget propval)Set `mnemonicWidget` property. Params: propval = The widget to be activated when the labels mnemonic key is pressed. Returns: Builder instance for fluent chaining
T naturalWrapMode(gtk.types.NaturalWrapMode propval)Set `naturalWrapMode` property. Params: propval = Select the line wrapping for the natural size request.
T selectable(bool propval)Set `selectable` property. Params: propval = Whether the label text can be selected with the mouse. Returns: Builder instance for fluent chaining
T singleLineMode(bool propval)Set `singleLineMode` property. Params: propval = Whether the label is in single line mode.
T tabs(pango.tab_array.TabArray propval)Set `tabs` property. Params: propval = Custom tabs for this label. Returns: Builder instance for fluent chaining
T useMarkup(bool propval)Set `useMarkup` property. Params: propval = true if the text of the label includes Pango markup.
T useUnderline(bool propval)Set `useUnderline` property. Params: propval = true if the text of the label indicates a mnemonic with an _ before the mnemonic character. Returns: Builder instance for fluent chaining
T widthChars(int propval)Set `widthChars` property. Params: propval = The desired width of the label, in characters.
T wrap(bool propval)Set `wrap` property. Params: propval = true if the label text will wrap if it gets too wide. Returns: Builder instance for fluent chaining
T wrapMode(pango.types.WrapMode propval)Set `wrapMode` property. Params: propval = Controls how the line wrapping is done.
T xalign(float propval)Set `xalign` property. Params: propval = The horizontal alignment of the label text inside its size allocation.
T yalign(float propval)Set `yalign` property. Params: propval = The vertical alignment of the label text inside its size allocation.

Fluent builder for [gtk.label.Label]

Methods
Label build()