gtk.label

Module for [Label] class

Types 3

The #GtkLabel widget displays a small amount of text. As the name implies, most labels are used to label another widget such as a #GtkButton, a #GtkMenuItem, or a #GtkComboBox.

CSS nodes

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

GtkLabel 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 #GtkShortcutsWindow, labels are used wth 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.

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 #PangoAttribute 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.

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 #GtkButton; 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_container_add (GTK_CONTAINER (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 #GtkEntry, 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 complete documentation of available tags in the Pango manual.)

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 glib.global.markupEscapeText or glib.global.markupPrintfEscaped.

Markup strings are just a convenient way to set the #PangoAttrList on a label; gtk.label.Label.setAttributes may be a simpler way to set attributes in some cases. Be careful though; #PangoAttrList 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 #PangoAttribute 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 # {#label-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.setLineWrap.

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 #GtkWidget:halign and #GtkWidget:valign properties.

The #GtkLabel:width-chars and #GtkLabel: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.

Note that the interpretation of #GtkLabel:width-chars and #GtkLabel:max-width-chars has changed a bit with the introduction of width-for-height geometry management.

Links

Since 2.18, 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“ and “title“ 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.

An example looks like this:

const gchar *text =
"Go to the"
"<a href=\"http://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 #GtkLabel::activate-link signal and the gtk.label.Label.getCurrentUri function.

Methods
GType _getGType() static nothrow
GType _gType() @property nothrow
Label self() nothrowReturns `this`, for use in `with` statements.
LabelGidBuilder builder() static nothrowGet builder for [gtk.label.Label] Returns: New builder object
double angle() @property nothrowGet `angle` property. Returns: The angle that the baseline of the label makes with the horizontal, in degrees, measured counterclockwise. An angle of 90 reads from from bottom to top, an angle of 2...
void angle(double propval) @property nothrowSet `angle` property. Params: propval = The angle that the baseline of the label makes with the horizontal, in degrees, measured counterclockwise. An angle of 90 reads from from bottom to top, an a...
void attributes(pango.attr_list.AttrList propval) @property nothrow
int cursorPosition() @property nothrow
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, specified as a #PangoEllipsizeMode.
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, specified as a #PangoEllipsizeMode.
gtk.types.Justification justify() @property nothrow
void justify(gtk.types.Justification propval) @property nothrow
string label() @property nothrowGet `label` property. Returns: The contents of the label.
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. This property has no effect if the label is not wrapping or ellipsized. Set this propert...
void lines(int propval) @property nothrowSet `lines` property. Params: propval = The number of lines to which an ellipsized, wrapping label should be limited. This property has no effect if the label is not wrapping or ellipsized. Set thi...
int maxWidthChars() @property nothrowGet `maxWidthChars` property. Returns: The desired maximum width of the label, in characters. If this property is set to -1, the width will be calculated automatically.
void maxWidthChars(int propval) @property nothrowSet `maxWidthChars` property. Params: propval = The desired maximum width of the label, in characters. If this property is set to -1, the width will be calculated automatically.
uint mnemonicKeyval() @property nothrow
gtk.widget.Widget mnemonicWidget() @property nothrow
void mnemonicWidget(gtk.widget.Widget propval) @property nothrow
void pattern(string propval) @property nothrow
bool selectable() @property nothrow
void selectable(bool propval) @property nothrow
int selectionBound() @property nothrow
bool singleLineMode() @property nothrowGet `singleLineMode` property. Returns: Whether the label is in single line mode. In single line mode, the height of the label does not depend on the actual text, it is always set to ascent + desce...
void singleLineMode(bool propval) @property nothrowSet `singleLineMode` property. Params: propval = Whether the label is in single line mode. In single line mode, the height of the label does not depend on the actual text, it is always set to ascen...
bool trackVisitedLinks() @property nothrowGet `trackVisitedLinks` property. Returns: Set this property to true to make the label track which links have been visited. It will then apply the #GTK_STATE_FLAG_VISITED when rendering this link, ...
void trackVisitedLinks(bool propval) @property nothrowSet `trackVisitedLinks` property. Params: propval = Set this property to true to make the label track which links have been visited. It will then apply the #GTK_STATE_FLAG_VISITED when rendering th...
bool useMarkup() @property nothrow
void useMarkup(bool propval) @property nothrow
bool useUnderline() @property nothrow
void useUnderline(bool propval) @property nothrow
int widthChars() @property nothrowGet `widthChars` property. Returns: The desired width of the label, in characters. If this property is set to -1, the width will be calculated automatically.
void widthChars(int propval) @property nothrowSet `widthChars` property. Params: propval = The desired width of the label, in characters. If this property is set to -1, the width will be calculated automatically.
bool wrap() @property nothrow
void wrap(bool propval) @property nothrow
pango.types.WrapMode wrapMode() @property nothrowGet `wrapMode` property. Returns: If line wrapping is on (see the #GtkLabel:wrap property) this controls how the line wrapping is done. The default is [pango.types.WrapMode.Word], which means wrap ...
void wrapMode(pango.types.WrapMode propval) @property nothrowSet `wrapMode` property. Params: propval = If line wrapping is on (see the #GtkLabel:wrap property) this controls how the line wrapping is done. The default is [pango.types.WrapMode.Word], which me...
float xalign() @property nothrowGet `xalign` property. Returns: The xalign property determines the horizontal aligment of the label text inside the labels size allocation. Compare this to #GtkWidget:halign, which determines how t...
void xalign(float propval) @property nothrowSet `xalign` property. Params: propval = The xalign property determines the horizontal aligment of the label text inside the labels size allocation. Compare this to #GtkWidget:halign, which determi...
float yalign() @property nothrowGet `yalign` property. Returns: The yalign property determines the vertical aligment of the label text inside the labels size allocation. Compare this to #GtkWidget:valign, which determines how the...
void yalign(float propval) @property nothrowSet `yalign` property. Params: propval = The yalign property determines the vertical aligment of the label text inside the labels size allocation. Compare this to #GtkWidget:valign, which determine...
gtk.label.Label newWithMnemonic(string str = null) static nothrowCreates a new #GtkLabel, containing the text in str.
double getAngle() nothrowGets the angle of rotation for the label. See [gtk.label.Label.setAngle]. Returns: the angle of rotation for the label
pango.attr_list.AttrList getAttributes() nothrowGets the attribute list that was set on the label using [gtk.label.Label.setAttributes], if any. This function does not reflect attributes that come from the labels markup (see [gtk.label.Label.set...
string getCurrentUri() nothrowReturns the URI for the currently active link in the label. The active link is the one under the mouse pointer or, in a selectable label, the link in which the text cursor is currently positioned.
pango.types.EllipsizeMode getEllipsize() nothrowReturns the ellipsizing position of the label. See [gtk.label.Label.setEllipsize]. Returns: #PangoEllipsizeMode
gtk.types.Justification getJustify() nothrowReturns the justification of the label. See [gtk.label.Label.setJustify]. Returns: #GtkJustification
string getLabel() nothrowFetches the text from a label widget including any embedded underlines indicating mnemonics and Pango markup. (See [gtk.label.Label.getText]). Returns: the text of the label widget. This string is ...
pango.layout.Layout getLayout() nothrowGets the #PangoLayout used to display the label. The layout is useful to e.g. convert text positions to pixel positions, in combination with [gtk.label.Label.getLayoutOffsets]. The returned layout ...
void getLayoutOffsets(out int x, out int y) nothrowObtains the coordinates where the label will draw the #PangoLayout representing the text in the label; useful to convert mouse events into coordinates inside the #PangoLayout, e.g. to take some act...
bool getLineWrap() nothrowReturns whether lines in the label are automatically wrapped. See [gtk.label.Label.setLineWrap]. Returns: true if the lines of the label are automatically wrapped.
pango.types.WrapMode getLineWrapMode() nothrowReturns line wrap mode used by the label. See [gtk.label.Label.setLineWrapMode]. Returns: true if the lines of the label are automatically wrapped.
int getLines() nothrowGets the number of lines to which an ellipsized, wrapping label should be limited. See [gtk.label.Label.setLines]. Returns: The number of lines
int getMaxWidthChars() nothrowRetrieves the desired maximum width of label, in characters. See [gtk.label.Label.setWidthChars]. Returns: the maximum width of the label in characters.
uint getMnemonicKeyval() nothrowIf the label has been set so that it has an mnemonic key this function returns the keyval used for the mnemonic accelerator. If there is no mnemonic set up it returns #GDK_KEY_VoidSymbol. Returns: ...
gtk.widget.Widget getMnemonicWidget() nothrowRetrieves the target of the mnemonic (keyboard shortcut) of this label. See [gtk.label.Label.setMnemonicWidget]. Returns: the target of the label’s mnemonic, or null if none has been set and the ...
bool getSelectable() nothrowGets the value set by [gtk.label.Label.setSelectable]. Returns: true if the user can copy text from the label
bool getSelectionBounds(out int start, out int end) nothrowGets the selected range of characters in the label, returning true if there’s a selection.
bool getSingleLineMode() nothrowReturns whether the label is in single line mode. Returns: true when the label is in single line mode.
string getText() nothrowFetches the text from a label widget, as displayed on the screen. This does not include any embedded underlines indicating mnemonics or Pango markup. (See [gtk.label.Label.getLabel]) Returns: the t...
bool getTrackVisitedLinks() nothrowReturns whether the label is currently keeping track of clicked links. Returns: true if clicked links are remembered
bool getUseMarkup() nothrowReturns whether the label’s text is interpreted as marked up with the [Pango text markup language][PangoMarkupFormat]. See gtk_label_set_use_markup (). Returns: true if the label’s text will be...
bool getUseUnderline() nothrowReturns whether an embedded underline in the label indicates a mnemonic. See [gtk.label.Label.setUseUnderline]. Returns: true whether an embedded underline in the label indicates the mnemonic accel...
int getWidthChars() nothrowRetrieves the desired width of label, in characters. See [gtk.label.Label.setWidthChars]. Returns: the width of the label in characters.
float getXalign() nothrowGets the #GtkLabel:xalign property for label. Returns: the xalign property
float getYalign() nothrowGets the #GtkLabel:yalign property for label. Returns: the yalign property
void selectRegion(int startOffset, int endOffset) nothrowSelects a range of characters in the label, if the label is selectable. See [gtk.label.Label.setSelectable]. If the label is not selectable, this function has no effect. If start_offset or end_offs...
void setAngle(double angle) nothrowSets the angle of rotation for the label. An angle of 90 reads from from bottom to top, an angle of 270, from top to bottom. The angle setting for the label is ignored if the label is selectable, w...
void setAttributes(pango.attr_list.AttrList attrs = null) nothrowSets a #PangoAttrList; the attributes in the list are applied to the label text.
void setEllipsize(pango.types.EllipsizeMode mode) nothrowSets the mode used to ellipsize (add an ellipsis: "...") to the text if there is not enough space to render the entire string.
void setJustify(gtk.types.Justification jtype) nothrowSets the alignment of the lines in the text of the label relative to each other. [gtk.types.Justification.Left] is the default value when the widget is first created with [gtk.label.Label.new_]. If...
void setLabel(string str) nothrowSets the text of the label. The label is interpreted as including embedded underlines and/or Pango markup depending on the values of the #GtkLabel:use-underline and #GtkLabel:use-markup properties.
void setLineWrap(bool wrap) nothrowToggles line wrapping within the #GtkLabel widget. true makes it break lines if text exceeds the widget’s size. false lets the text get cut off by the edge of the widget if it exceeds the widget ...
void setLineWrapMode(pango.types.WrapMode wrapMode) nothrowIf line wrapping is on (see [gtk.label.Label.setLineWrap]) this controls how the line wrapping is done. The default is [pango.types.WrapMode.Word] which means wrap on word boundaries.
void setLines(int lines) nothrowSets the number of lines to which an ellipsized, wrapping label should be limited. This has no effect if the label is not wrapping or ellipsized. Set this to -1 if you don’t want to limit the num...
void setMarkup(string str) nothrowParses str which is marked up with the [Pango text markup language][PangoMarkupFormat], setting the label’s text and attribute list based on the parse results.
void setMarkupWithMnemonic(string str) nothrowParses str which is marked up with the [Pango text markup language][PangoMarkupFormat], setting the label’s text and attribute list based on the parse results. If characters in str are preceded b...
void setMaxWidthChars(int nChars) nothrowSets the desired maximum width in characters of label to n_chars.
void setMnemonicWidget(gtk.widget.Widget widget = null) nothrowIf the label has been set so that it has an mnemonic key (using i.e. [gtk.label.Label.setMarkupWithMnemonic], [gtk.label.Label.setTextWithMnemonic], [gtk.label.Label.newWithMnemonic] or the “use_...
void setPattern(string pattern) nothrowThe pattern of underlines you want under the existing text within the #GtkLabel widget. For example if the current text of the label says “FooBarBaz” passing a pattern of “___ ___” will ...
void setSelectable(bool setting) nothrowSelectable labels allow the user to select text from the label, for copy-and-paste.
void setSingleLineMode(bool singleLineMode) nothrowSets whether the label is in single line mode.
void setText(string str) nothrowSets the text within the #GtkLabel widget. It overwrites any text that was there before.
void setTextWithMnemonic(string str) nothrowSets the label’s text from the string str. If characters in str are preceded by an underscore, they are underlined indicating that they represent a keyboard accelerator called a mnemonic. The mne...
void setTrackVisitedLinks(bool trackLinks) nothrowSets whether the label should keep track of clicked links (and use a different color for them).
void setUseMarkup(bool setting) nothrowSets whether the text of the label contains markup in [Pango’s text markup language][PangoMarkupFormat]. See [gtk.label.Label.setMarkup].
void setUseUnderline(bool setting) nothrowIf true, an underline in the text indicates the next character should be used for the mnemonic accelerator key.
void setWidthChars(int nChars) nothrowSets the desired width in characters of label to n_chars.
void setXalign(float xalign) nothrowSets the #GtkLabel:xalign property for label.
void setYalign(float yalign) nothrowSets the #GtkLabel:yalign property for 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) 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.
gulong connectPopulatePopup(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.menu.Menu))) && (Parameters!T.length < 2 || (ParameterStorageClassTuple!T[1] == ParameterStorageClass.none && is(Parameters!T[1] : gtk.label.Label))) && Parameters!T.length < 3) nothrowConnect to `PopulatePopup` signal.
Constructors
this(void * ptr, Flag!"Take" take)
this(string str = null)Creates a new label with the given text inside it. You can pass null to get an empty label widget.

Fluent builder implementation template for gtk.label.Label

Methods
T angle(double propval) nothrowSet `angle` property. Params: propval = The angle that the baseline of the label makes with the horizontal, in degrees, measured counterclockwise. An angle of 90 reads from from bottom to top, an a...
T 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, specified as a #PangoEllipsizeMode.
T justify(gtk.types.Justification propval) nothrow
T label(string propval) nothrowSet `label` property. Params: propval = The contents of the label.
T lines(int propval) nothrowSet `lines` property. Params: propval = The number of lines to which an ellipsized, wrapping label should be limited. This property has no effect if the label is not wrapping or ellipsized. Set thi...
T maxWidthChars(int propval) nothrowSet `maxWidthChars` property. Params: propval = The desired maximum width of the label, in characters. If this property is set to -1, the width will be calculated automatically.
T pattern(string propval) nothrow
T selectable(bool propval) nothrow
T singleLineMode(bool propval) nothrowSet `singleLineMode` property. Params: propval = Whether the label is in single line mode. In single line mode, the height of the label does not depend on the actual text, it is always set to ascen...
T trackVisitedLinks(bool propval) nothrowSet `trackVisitedLinks` property. Params: propval = Set this property to true to make the label track which links have been visited. It will then apply the #GTK_STATE_FLAG_VISITED when rendering th...
T useMarkup(bool propval) nothrow
T useUnderline(bool propval) nothrow
T widthChars(int propval) nothrowSet `widthChars` property. Params: propval = The desired width of the label, in characters. If this property is set to -1, the width will be calculated automatically.
T wrap(bool propval) nothrow
T wrapMode(pango.types.WrapMode propval) nothrowSet `wrapMode` property. Params: propval = If line wrapping is on (see the #GtkLabel:wrap property) this controls how the line wrapping is done. The default is [pango.types.WrapMode.Word], which me...
T xalign(float propval) nothrowSet `xalign` property. Params: propval = The xalign property determines the horizontal aligment of the label text inside the labels size allocation. Compare this to #GtkWidget:halign, which determi...
T yalign(float propval) nothrowSet `yalign` property. Params: propval = The yalign property determines the vertical aligment of the label text inside the labels size allocation. Compare this to #GtkWidget:valign, which determine...

Fluent builder for gtk.label.Label

Methods
Label build() nothrowCreate object from builder. Returns: New object