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 gtk.button.Button.
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 gtk.label.Label.widthChars and gtk.label.Label.maxWidthChars 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 gtk.label.Label.activateLink signal and the gtk.label.Label.getCurrentUri function.
LabelGidBuilder builder() static nothrowGet builder for [gtk.label.Label] Returns: New builder objectpango.attr_list.AttrList attributes() @property nothrowGet `attributes` property. Returns: A list of style attributes to apply to the text of the label.void attributes(pango.attr_list.AttrList propval) @property nothrowSet `attributes` property. Params: propval = A list of style attributes to apply to the text of the label.pango.types.EllipsizeMode ellipsize() @property nothrowGet `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) @property nothrowSet `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() @property nothrowGet `extraMenu` property. Returns: A menu model whose contents will be appended to the context menu.void extraMenu(gio.menu_model.MenuModel propval) @property nothrowSet `extraMenu` property. Params: propval = A menu model whose contents will be appended to the context menu.gtk.types.Justification justify() @property nothrowGet `justify` property. Returns: The alignment of the lines in the text of the label, relative to each other.void justify(gtk.types.Justification propval) @property nothrowSet `justify` property. Params: propval = The alignment of the lines in the text of the label, relative to each other.void label(string propval) @property nothrowSet `label` property. Params: propval = The contents of the label.int lines() @property nothrowGet `lines` property. Returns: The number of lines to which an ellipsized, wrapping label should be limited.void lines(int propval) @property nothrowSet `lines` property. Params: propval = The number of lines to which an ellipsized, wrapping label should be limited.int maxWidthChars() @property nothrowGet `maxWidthChars` property. Returns: The desired maximum width of the label, in characters.void maxWidthChars(int propval) @property nothrowSet `maxWidthChars` property. Params: propval = The desired maximum width of the label, in characters.uint mnemonicKeyval() @property nothrowGet `mnemonicKeyval` property. Returns: The mnemonic accelerator key for the label.gtk.widget.Widget mnemonicWidget() @property nothrowGet `mnemonicWidget` property. Returns: The widget to be activated when the labels mnemonic key is pressed.void mnemonicWidget(gtk.widget.Widget propval) @property nothrowSet `mnemonicWidget` property. Params: propval = The widget to be activated when the labels mnemonic key is pressed.gtk.types.NaturalWrapMode naturalWrapMode() @property nothrowGet `naturalWrapMode` property. Returns: Select the line wrapping for the natural size request.void naturalWrapMode(gtk.types.NaturalWrapMode propval) @property nothrowSet `naturalWrapMode` property. Params: propval = Select the line wrapping for the natural size request.bool selectable() @property nothrowGet `selectable` property. Returns: Whether the label text can be selected with the mouse.void selectable(bool propval) @property nothrowSet `selectable` property. Params: propval = Whether the label text can be selected with the mouse.bool singleLineMode() @property nothrowGet `singleLineMode` property. Returns: Whether the label is in single line mode.void singleLineMode(bool propval) @property nothrowSet `singleLineMode` property. Params: propval = Whether the label is in single line mode.pango.tab_array.TabArray tabs() @property nothrowGet `tabs` property. Returns: Custom tabs for this label.void tabs(pango.tab_array.TabArray propval) @property nothrowSet `tabs` property. Params: propval = Custom tabs for this label.bool useMarkup() @property nothrowGet `useMarkup` property. Returns: true if the text of the label includes Pango markup.void useMarkup(bool propval) @property nothrowSet `useMarkup` property. Params: propval = true if the text of the label includes Pango markup.bool useUnderline() @property nothrowGet `useUnderline` property. Returns: true if the text of the label indicates a mnemonic with an _ before the mnemonic character.void useUnderline(bool propval) @property nothrowSet `useUnderline` property. Params: propval = true if the text of the label indicates a mnemonic with an _ before the mnemonic character.int widthChars() @property nothrowGet `widthChars` property. Returns: The desired width of the label, in characters.void widthChars(int propval) @property nothrowSet `widthChars` property. Params: propval = The desired width of the label, in characters.bool wrap() @property nothrowGet `wrap` property. Returns: true if the label text will wrap if it gets too wide.void wrap(bool propval) @property nothrowSet `wrap` property. Params: propval = true if the label text will wrap if it gets too wide.pango.types.WrapMode wrapMode() @property nothrowGet `wrapMode` property. Returns: Controls how the line wrapping is done.void wrapMode(pango.types.WrapMode propval) @property nothrowSet `wrapMode` property. Params: propval = Controls how the line wrapping is done.float xalign() @property nothrowGet `xalign` property. Returns: The horizontal alignment of the label text inside its size allocation.void xalign(float propval) @property nothrowSet `xalign` property. Params: propval = The horizontal alignment of the label text inside its size allocation.float yalign() @property nothrowGet `yalign` property. Returns: The vertical alignment of the label text inside its size allocation.void yalign(float propval) @property nothrowSet `yalign` property. Params: propval = The vertical alignment of the label text inside its size allocation.gtk.label.Label newWithMnemonic(string str = null) static nothrowCreates a new [gtk.label.Label], containing the text in str.void getLayoutOffsets(out int x, out int y) nothrowObtains the coordinates where the label will draw its [pango.layout.Layout].int getLines() nothrowGets the number of lines to which an ellipsized, wrapping label should be limited.gtk.widget.Widget getMnemonicWidget() nothrowRetrieves the target of the mnemonic (keyboard shortcut) of this label.bool getSelectable() nothrowReturns whether the label is selectable. Returns: true if the user can copy text from the labelbool getSelectionBounds(out int start, out int end) nothrowGets the selected range of characters in the label.bool getSingleLineMode() nothrowReturns whether the label is in single line mode. Returns: true when the label is in single line mode.bool getUseUnderline() nothrowReturns whether an embedded underlines in the label indicate mnemonics.void selectRegion(int startOffset, int endOffset) nothrowSelects a range of characters in the label, if the label is selectable.void setAttributes(pango.attr_list.AttrList attrs = null) nothrowApply attributes to the label text.void setExtraMenu(gio.menu_model.MenuModel model = null) nothrowSets a menu model to add when constructing the context menu for label.void setJustify(gtk.types.Justification jtype) nothrowSets the alignment of the lines in the text of the label relative to each other.void setLines(int lines) nothrowSets the number of lines to which an ellipsized, wrapping label should be limited.void setMarkupWithMnemonic(string str) nothrowSets the labels text, attributes and mnemonic from markup.void setMaxWidthChars(int nChars) nothrowSets the desired maximum width in characters of label to n_chars.void setMnemonicWidget(gtk.widget.Widget widget = null) nothrowAssociate the label with its mnemonic target.void setNaturalWrapMode(gtk.types.NaturalWrapMode wrapMode) nothrowSelect the line wrapping for the natural size request.void setTabs(pango.tab_array.TabArray tabs = null) nothrowSets the default tab stops for paragraphs in self.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) nothrowConnect 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) nothrowConnect 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) nothrowConnect 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) nothrowConnect to `MoveCursor` signal.Fluent builder implementation template for gtk.label.Label
T attributes(pango.attr_list.AttrList propval) nothrowSet `attributes` property. Params: propval = A list of style attributes to apply to the text of the label. Returns: Builder instance for fluent chainingT ellipsize(pango.types.EllipsizeMode propval) nothrowSet `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) nothrowSet `extraMenu` property. Params: propval = A menu model whose contents will be appended to the context menu. Returns: Builder instance for fluent chainingT justify(gtk.types.Justification propval) nothrowSet `justify` property. Params: propval = The alignment of the lines in the text of the label, relative to each other.T lines(int propval) nothrowSet `lines` property. Params: propval = The number of lines to which an ellipsized, wrapping label should be limited.T maxWidthChars(int propval) nothrowSet `maxWidthChars` property. Params: propval = The desired maximum width of the label, in characters.T mnemonicWidget(gtk.widget.Widget propval) nothrowSet `mnemonicWidget` property. Params: propval = The widget to be activated when the labels mnemonic key is pressed. Returns: Builder instance for fluent chainingT naturalWrapMode(gtk.types.NaturalWrapMode propval) nothrowSet `naturalWrapMode` property. Params: propval = Select the line wrapping for the natural size request.T selectable(bool propval) nothrowSet `selectable` property. Params: propval = Whether the label text can be selected with the mouse. Returns: Builder instance for fluent chainingT singleLineMode(bool propval) nothrowSet `singleLineMode` property. Params: propval = Whether the label is in single line mode.T tabs(pango.tab_array.TabArray propval) nothrowSet `tabs` property. Params: propval = Custom tabs for this label. Returns: Builder instance for fluent chainingT useMarkup(bool propval) nothrowSet `useMarkup` property. Params: propval = true if the text of the label includes Pango markup.T useUnderline(bool propval) nothrowSet `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 chainingT widthChars(int propval) nothrowSet `widthChars` property. Params: propval = The desired width of the label, in characters.T wrap(bool propval) nothrowSet `wrap` property. Params: propval = true if the label text will wrap if it gets too wide. Returns: Builder instance for fluent chainingT wrapMode(pango.types.WrapMode propval) nothrowSet `wrapMode` property. Params: propval = Controls how the line wrapping is done.T xalign(float propval) nothrowSet `xalign` property. Params: propval = The horizontal alignment of the label text inside its size allocation.T yalign(float propval) nothrowSet `yalign` property. Params: propval = The vertical alignment of the label text inside its size allocation.Fluent builder for gtk.label.Label