Class

Gtk.Label

Description [src]

final class Gtk.Label : Gtk.Widget {
  /* No available fields */
}

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.

An example GtkLabel

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 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 element, which supports any number of elements. The element has attributes named “name“, “value“, “start“ and “end“ and allows you to specify Pango.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

GtkLabel uses the #GTK_ACCESSIBLE_ROLE_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_new_with_mnemonic() or gtk_label_set_text_with_mnemonic().

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 GtkLabel. 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 GtkEntry, you have to point the label at the entry with gtk_label_set_mnemonic_widget():

// 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, Pango.parse_markup)

The markup passed to gtk_label_set_markup() 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_set_markup(), you’ll want to escape it with g_markup_escape_text() or g_markup_printf_escaped().

Markup strings are just a convenient way to set the Pango.AttrList on a label; gtk_label_set_attributes() may be a simpler way to set attributes in some cases. Be careful though; Pango.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 requires knowledge of the exact string being displayed, so translations will cause problems.

Selectable labels

Labels can be made selectable with gtk_label_set_selectable(). 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_set_wrap().

gtk_label_set_justify() 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.

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 looks like this:

const char *text =
"Go to the"
"<a href=\"http://www.gtk.org title=\"&lt;i&gt;Our&lt;/i&gt; 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_get_current_uri() function.

Hierarchy

Hierarchy for Gtk.Label

Ancestors

Constructors

gtk_label_new

Creates a new label with the given text inside it.

gtk_label_new_with_mnemonic

Creates a new GtkLabel, containing the text in str.

Instance methods

gtk_label_get_attributes

Gets the labels attribute list.

gtk_label_get_current_uri

Returns the URI for the currently active link in the label.

gtk_label_get_ellipsize

Returns the ellipsizing position of the label.

gtk_label_get_extra_menu

Gets the extra menu model of label.

gtk_label_get_justify

Returns the justification of the label.

gtk_label_get_label

Fetches the text from a label.

gtk_label_get_layout

Gets the PangoLayout used to display the label.

gtk_label_get_layout_offsets

Obtains the coordinates where the label will draw its PangoLayout.

gtk_label_get_lines

Gets the number of lines to which an ellipsized, wrapping label should be limited.

gtk_label_get_max_width_chars

Retrieves the desired maximum width of label, in characters.

gtk_label_get_mnemonic_keyval

Return the mnemonic accelerator.

gtk_label_get_mnemonic_widget

Retrieves the target of the mnemonic (keyboard shortcut) of this label.

gtk_label_get_selectable

Returns whether the label is selectable.

gtk_label_get_selection_bounds

Gets the selected range of characters in the label.

gtk_label_get_single_line_mode

Returns whether the label is in single line mode.

gtk_label_get_text

Fetches the text from a label.

gtk_label_get_use_markup

Returns whether the label’s text is interpreted as Pango markup.

gtk_label_get_use_underline

Returns whether an embedded underlines in the label indicate mnemonics.

gtk_label_get_width_chars

Retrieves the desired width of label, in characters.

gtk_label_get_wrap

Returns whether lines in the label are automatically wrapped.

gtk_label_get_wrap_mode

Returns line wrap mode used by the label.

gtk_label_get_xalign

Gets the xalign of the label.

gtk_label_get_yalign

Gets the yalign of the label.

gtk_label_select_region

Selects a range of characters in the label, if the label is selectable.

gtk_label_set_attributes

Apply attributes to the label text.

gtk_label_set_ellipsize

Sets the mode used to ellipsizei the text.

gtk_label_set_extra_menu

Sets a menu model to add when constructing the context menu for label.

gtk_label_set_justify

Sets the alignment of the lines in the text of the label relative to each other.

gtk_label_set_label

Sets the text of the label.

gtk_label_set_lines

Sets the number of lines to which an ellipsized, wrapping label should be limited.

gtk_label_set_markup

Sets the labels text and attributes from markup.

gtk_label_set_markup_with_mnemonic

Sets the labels text, attributes and mnemonic from markup.

gtk_label_set_max_width_chars

Sets the desired maximum width in characters of label to n_chars.

gtk_label_set_mnemonic_widget

Associate the label with its mnemonic target.

gtk_label_set_selectable

Makes text in the label selectable.

gtk_label_set_single_line_mode

Sets whether the label is in single line mode.

gtk_label_set_text

Sets the text within the GtkLabel widget.

gtk_label_set_text_with_mnemonic

Sets the label’s text from the string str.

gtk_label_set_use_markup

Sets whether the text of the label contains markup.

gtk_label_set_use_underline

Sets whether underlines in the text indicate mnemonics.

gtk_label_set_width_chars

Sets the desired width in characters of label to n_chars.

gtk_label_set_wrap

Toggles line wrapping within the GtkLabel widget.

gtk_label_set_wrap_mode

Controls how line wrapping is done.

gtk_label_set_xalign

Sets the xalign of the label.

gtk_label_set_yalign

Sets the yalign of the label.

Methods inherited from GtkWidget (159)
Methods inherited from GtkAccessible (10)
gtk_accessible_get_accessible_role

Retrieves the GtkAccessibleRole for the given GtkAccessible.

gtk_accessible_reset_property

Resets the accessible property to its default value.

gtk_accessible_reset_relation

Resets the accessible relation to its default value.

gtk_accessible_reset_state

Resets the accessible state to its default value.

gtk_accessible_update_property

Updates a list of accessible properties.

gtk_accessible_update_property_value

Updates an array of accessible properties.

gtk_accessible_update_relation

Updates a list of accessible relations.

gtk_accessible_update_relation_value

Updates an array of accessible relations.

gtk_accessible_update_state

Updates a list of accessible states. See the GtkAccessibleState documentation for the value types of accessible states.

gtk_accessible_update_state_value

Updates an array of accessible states.

Methods inherited from GtkBuildable (1)
gtk_buildable_get_buildable_id

Gets the ID of the buildable object.

Properties

Gtk.Label:attributes

A list of style attributes to apply to the text of the label.

Gtk.Label:ellipsize

The preferred place to ellipsize the string, if the label does not have enough room to display the entire string.

Gtk.Label:extra-menu

A menu model whose contents will be appended to the context menu.

Gtk.Label:justify

The alignment of the lines in the text of the label, relative to each other.

Gtk.Label:label

The contents of the label.

Gtk.Label:lines

The number of lines to which an ellipsized, wrapping label should be limited.

Gtk.Label:max-width-chars

The desired maximum width of the label, in characters.

Gtk.Label:mnemonic-keyval

The mnemonic accelerator key for the label.

Gtk.Label:mnemonic-widget

The widget to be activated when the labels mnemonic key is pressed.

Gtk.Label:selectable

Whether the label text can be selected with the mouse.

Gtk.Label:single-line-mode

Whether the label is in single line mode.

Gtk.Label:use-markup

TRUE if the text of the label includes Pango markup.

Gtk.Label:use-underline

TRUE if the text of the label indicates a mnemonic with _.

Gtk.Label:width-chars

The desired width of the label, in characters.

Gtk.Label:wrap

TRUE if the label text will wrap if it gets too wide.

Gtk.Label:wrap-mode

Controls how the line wrapping is done.

Gtk.Label:xalign

The horizontal alignment of the label text inside its size allocation.

Gtk.Label:yalign

The vertical alignment of the label text inside its size allocation.

Properties inherited from GtkWidget (34)
Gtk.Widget:can-focus

Whether the widget or any of its descendents can accept the input focus.

Gtk.Widget:can-target

Whether the widget can receive pointer events.

Gtk.Widget:css-classes

A list of css classes applied to this widget.

Gtk.Widget:css-name

The name of this widget in the CSS tree.

Gtk.Widget:cursor

The cursor used by widget.

Gtk.Widget:focus-on-click

Whether the widget should grab focus when it is clicked with the mouse.

Gtk.Widget:focusable

Whether this widget itself will accept the input focus.

Gtk.Widget:halign

How to distribute horizontal space if widget gets extra space.

Gtk.Widget:has-default

Whether the widget is the default widget.

Gtk.Widget:has-focus

Whether the widget has the input focus.

Gtk.Widget:has-tooltip

Enables or disables the emission of the ::query-tooltip signal on widget.

Gtk.Widget:height-request

Override for height request of the widget.

Gtk.Widget:hexpand

Whether to expand horizontally.

Gtk.Widget:hexpand-set

Whether to use the hexpand property.

Gtk.Widget:layout-manager

The GtkLayoutManager instance to use to compute the preferred size of the widget, and allocate its children.

Gtk.Widget:margin-bottom

Margin on bottom side of widget.

Gtk.Widget:margin-end

Margin on end of widget, horizontally.

Gtk.Widget:margin-start

Margin on start of widget, horizontally.

Gtk.Widget:margin-top

Margin on top side of widget.

Gtk.Widget:name

The name of the widget.

Gtk.Widget:opacity

The requested opacity of the widget.

Gtk.Widget:overflow

How content outside the widget’s content area is treated.

Gtk.Widget:parent

The parent widget of this widget.

Gtk.Widget:receives-default

Whether the widget will receive the default action when it is focused.

Gtk.Widget:root

The GtkRoot widget of the widget tree containing this widget.

Gtk.Widget:scale-factor

The scale factor of the widget.

Gtk.Widget:sensitive

Whether the widget responds to input.

Gtk.Widget:tooltip-markup

Sets the text of tooltip to be the given string, which is marked up with Pango markup.

Gtk.Widget:tooltip-text

Sets the text of tooltip to be the given string.

Gtk.Widget:valign

How to distribute vertical space if widget gets extra space.

Gtk.Widget:vexpand

Whether to expand vertically.

Gtk.Widget:vexpand-set

Whether to use the vexpand property.

Gtk.Widget:visible

Whether the widget is visible.

Gtk.Widget:width-request

Override for width request of the widget.

Properties inherited from GtkAccessible (1)
Gtk.Accessible:accessible-role

The accessible role of the given GtkAccessible implementation.

Signals

Gtk.Label::activate-current-link

Gets emitted when the user activates a link in the label.

Gtk.Label::activate-link

Gets emitted to activate a URI.

Gtk.Label::copy-clipboard

Gets emitted to copy the slection to the clipboard.

Gtk.Label::move-cursor

Gets emitted when the user initiates a cursor movement.

Signals inherited from GtkWidget (13)
Gtk.Widget::destroy

Signals that all holders of a reference to the widget should release the reference that they hold.

Gtk.Widget::direction-changed

Emitted when the text direction of a widget changes.

Gtk.Widget::hide

Emitted when widget is hidden.

Gtk.Widget::keynav-failed

Emitted if keyboard navigation fails.

Gtk.Widget::map

Emitted when widget is going to be mapped.

Gtk.Widget::mnemonic-activate

Emitted when a widget is activated via a mnemonic.

Gtk.Widget::move-focus

Emitted when the focus is moved.

Gtk.Widget::query-tooltip

Emitted when the widgets tooltip is about to be shown.

Gtk.Widget::realize

Emitted when widget is associated with a GdkSurface.

Gtk.Widget::show

Emitted when widget is shown.

Gtk.Widget::state-flags-changed

Emitted when the widget state changes.

Gtk.Widget::unmap

Emitted when widget is going to be unmapped.

Gtk.Widget::unrealize

Emitted when the GdkSurface associated with widget is destroyed.