Class

GtkLabel

Description [src]

final class Gtk.Label : Gtk.Widget
  implements Gtk.Accessible, Gtk.AccessibleText, Gtk.Buildable, Gtk.ConstraintTarget {
  /* 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

Shortcuts and Gestures

GtkLabel supports the following keyboard shortcuts, when the cursor is visible:

  • Shift+F10 or Menu opens the context menu.
  • Ctrl+A or Ctrl+/ selects all.
  • Ctrl+Shift+A or Ctrl+\ unselects all.

Additionally, the following signals have default keybindings:

Actions

GtkLabel defines a set of built-in actions:

  • clipboard.copy copies the text to the clipboard.
  • clipboard.cut doesn’t do anything, since text in labels can’t be deleted.
  • clipboard.paste doesn’t do anything, since text in labels can’t be edited.
  • link.open opens the link, when activated on a link inside the label.
  • link.copy copies the link to the clipboard, when activated on a link inside the label.
  • menu.popup opens the context menu.
  • selection.delete doesn’t do anything, since text in labels can’t be deleted.
  • selection.select-all selects all of the text, if the label allows selection.

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

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

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 &lt;, &gt;, and &amp;. 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 PangoAttrList on a label; gtk_label_set_attributes() 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_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.

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=\"&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 this GtkLabel implements_0 GtkAccessible this--implements_0 implements_1 GtkAccessibleText this--implements_1 implements_2 GtkBuildable this--implements_2 implements_3 GtkConstraintTarget this--implements_3 ancestor_0 GtkWidget ancestor_0--this ancestor_1 GInitiallyUnowned ancestor_1--ancestor_0 ancestor_2 GObject ancestor_2--ancestor_1

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 label’s 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_natural_wrap_mode

Returns line wrap mode used by the label.

since: 4.6

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_tabs

Gets the tabs for self.

since: 4.8

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 ellipsize 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_natural_wrap_mode

Select the line wrapping for the natural size request.

since: 4.6

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_tabs

Sets the default tab stops for paragraphs in self.

since: 4.8

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 (162)

Please see GtkWidget for a full list of methods.

Methods inherited from GObject (43)

Please see GObject for a full list of methods.

Methods inherited from GtkAccessible (19)
gtk_accessible_announce

Requests the user’s screen reader to announce the given message.

since: 4.14

gtk_accessible_get_accessible_parent

Retrieves the accessible parent for an accessible object.

since: 4.10

gtk_accessible_get_accessible_role

Retrieves the accessible role of an accessible object.

gtk_accessible_get_at_context

Retrieves the accessible implementation for the given GtkAccessible.

since: 4.10

gtk_accessible_get_bounds

Queries the coordinates and dimensions of this accessible.

since: 4.10

gtk_accessible_get_first_accessible_child

Retrieves the first accessible child of an accessible object.

since: 4.10

gtk_accessible_get_next_accessible_sibling

Retrieves the next accessible sibling of an accessible object.

since: 4.10

gtk_accessible_get_platform_state

Query a platform state, such as focus.

since: 4.10

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_set_accessible_parent

Sets the parent and sibling of an accessible object.

since: 4.10

gtk_accessible_update_next_accessible_sibling

Updates the next accessible sibling of self.

since: 4.10

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 GtkAccessibleText (3)
gtk_accessible_text_update_caret_position

Updates the position of the caret.

since: 4.14

gtk_accessible_text_update_contents

Notifies assistive technologies of a change in contents.

since: 4.14

gtk_accessible_text_update_selection_bound

Updates the boundary of the selection.

since: 4.14

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:natural-wrap-mode

Select the line wrapping for the natural size request.

since: 4.6

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:tabs

Custom tabs for this label.

since: 4.8

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 an _ before the mnemonic character.

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)
GtkAccessible: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 selection to the clipboard.

Gtk.Label::move-cursor

Gets emitted when the user initiates a cursor movement.

Signals inherited from GtkWidget (13)
GtkWidget::destroy

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

GtkWidget::direction-changed

Emitted when the text direction of a widget changes.

GtkWidget::hide

Emitted when widget is hidden.

GtkWidget::keynav-failed

Emitted if keyboard navigation fails.

GtkWidget::map

Emitted when widget is going to be mapped.

GtkWidget::mnemonic-activate

Emitted when a widget is activated via a mnemonic.

GtkWidget::move-focus

Emitted when the focus is moved.

GtkWidget::query-tooltip

Emitted when the widget’s tooltip is about to be shown.

GtkWidget::realize

Emitted when widget is associated with a GdkSurface.

GtkWidget::show

Emitted when widget is shown.

GtkWidget::state-flags-changed

Emitted when the widget state changes.

GtkWidget::unmap

Emitted when widget is going to be unmapped.

GtkWidget::unrealize

Emitted when the GdkSurface associated with widget is destroyed.

Signals inherited from GObject (1)
GObject::notify

The notify signal is emitted on an object when one of its properties has its value set through g_object_set_property(), g_object_set(), et al.