The `GtkLabel` 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](label.png)
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 [[email protected]] values for this label.
An example of a UI definition fragment specifying Pango attributes: ```xml <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 [
[email protected]_with_mnemonic] or [[email protected]_text_with_mnemonic].
Mnemonics automatically activate any activatable widget the label is inside, such as a [[email protected]]; if the label is not inside the
mnemonic’s target widget, you have to tell the label about the target using [[email protected]_mnemonic_widget]. Here’s a simple
example where the label is inside a button:
```c // 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:
```c // 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 [[email protected]], you have to point the label at the entry with [
[email protected]_mnemonic_widget]:
```c // 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: ```c 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, [[email protected]_markup])
The markup passed to 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 [
[email protected]_markup], you’ll want to escape it with escape_text or
printf_escaped.
Markup strings are just a convenient way to set the [[email protected]] on a label; [[email protected]_attributes] may be a
simpler way to set attributes in some cases. Be careful though; [[email protected]] 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 [[email protected]]
requires knowledge of the exact string being displayed, so translations will cause problems.
Selectable labels
Labels can be made selectable with [[email protected]_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 [[email protected]_wrap].
[[email protected]_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 [[email protected]:halign] and [[email protected]:valign] properties.
The [[email protected]:width-chars] and [[email protected]: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:
```c const char *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 [[email protected]:GtkLabel:activate-link
]
signal and the [[email protected]_current_uri] function.
- public unowned AttrList? get_attributes ()
Gets the labels attribute list.
- public unowned string? get_current_uri ()
Returns the URI for the currently active link in the label.
- public EllipsizeMode get_ellipsize ()
Returns the ellipsizing position of the label.
- public unowned MenuModel? get_extra_menu ()
Gets the extra menu model of label
.
- public Justification get_justify ()
Returns the justification of the label.
- public unowned string get_label ()
Fetches the text from a label.
- public unowned Layout get_layout ()
Gets the `PangoLayout` used to display the label.
- public void get_layout_offsets (out int x, out int y)
Obtains the coordinates where the label will draw its `PangoLayout`.
- public int get_lines ()
Gets the number of lines to which an ellipsized, wrapping label should
be limited.
- public int get_max_width_chars ()
Retrieves the desired maximum width of label
, in
characters.
- public uint get_mnemonic_keyval ()
Return the mnemonic accelerator.
- public unowned Widget? get_mnemonic_widget ()
Retrieves the target of the mnemonic (keyboard shortcut) of this
label.
- public NaturalWrapMode get_natural_wrap_mode ()
Returns line wrap mode used by the label.
- public bool get_selectable ()
Returns whether the label is selectable.
- public bool get_selection_bounds (out int start, out int end)
Gets the selected range of characters in the label.
- public bool get_single_line_mode ()
Returns whether the label is in single line mode.
- public TabArray? get_tabs ()
Gets the tabs for this.
- public unowned string get_text ()
Fetches the text from a label.
- public bool get_use_markup ()
Returns whether the label’s text is interpreted as Pango markup.
- public bool get_use_underline ()
Returns whether an embedded underlines in the label indicate
mnemonics.
- public int get_width_chars ()
Retrieves the desired width of label
, in characters.
- public bool get_wrap ()
Returns whether lines in the label are automatically wrapped.
- public WrapMode get_wrap_mode ()
Returns line wrap mode used by the label.
- public float get_xalign ()
Gets the `xalign` of the label.
- public float get_yalign ()
Gets the `yalign` of the label.
- public void select_region (int start_offset, int end_offset)
Selects a range of characters in the label, if the label is
selectable.
- public void set_attributes (AttrList? attrs)
Apply attributes to the label text.
- public void set_ellipsize (EllipsizeMode mode)
Sets the mode used to ellipsizei the text.
- public void set_extra_menu (MenuModel? model)
Sets a menu model to add when constructing the context menu for
label
.
- public void set_justify (Justification jtype)
Sets the alignment of the lines in the text of the label relative to
each other.
- public void set_label (string str)
Sets the text of the label.
- public void set_lines (int lines)
Sets the number of lines to which an ellipsized, wrapping label should
be limited.
- public void set_markup (string str)
Sets the labels text and attributes from markup.
- public void set_markup_with_mnemonic (string str)
Sets the labels text, attributes and mnemonic from markup.
- public void set_max_width_chars (int n_chars)
Sets the desired maximum width in characters of label
to
n_chars
.
- public void set_mnemonic_widget (Widget? widget)
Associate the label with its mnemonic target.
- public void set_natural_wrap_mode (NaturalWrapMode wrap_mode)
Select the line wrapping for the natural size request.
- public void set_selectable (bool setting)
Makes text in the label selectable.
- public void set_single_line_mode (bool single_line_mode)
Sets whether the label is in single line mode.
- public void set_tabs (TabArray? tabs)
Sets the default tab stops for paragraphs in
this.
- public void set_text (string str)
Sets the text within the `GtkLabel` widget.
- public void set_text_with_mnemonic (string str)
Sets the label’s text from the string str
.
- public void set_use_markup (bool setting)
Sets whether the text of the label contains markup.
- public void set_use_underline (bool setting)
Sets whether underlines in the text indicate mnemonics.
- public void set_width_chars (int n_chars)
Sets the desired width in characters of label
to
n_chars
.
- public void set_wrap (bool wrap)
Toggles line wrapping within the `GtkLabel` widget.
- public void set_wrap_mode (WrapMode wrap_mode)
Controls how line wrapping is done.
- public void set_xalign (float xalign)
Sets the `xalign` of the label.
- public void set_yalign (float yalign)
Sets the `yalign` of the label.