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 [gtk.ShortcutsWindow.ShortcutsWindow|gtk.ShortcutsWindow], 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|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.
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 Label.newWithMnemonic or Label.setTextWithMnemonic.
Mnemonics automatically activate any activatable widget the label is inside, such as a gtk.Button; if the label is not inside the mnemonic’s target widget, you have to tell the label about the target using 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");
// 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);
To make it easy to format text in a label (changing colors, fonts, etc.), label text can be provided in a simple [markup format]PangoMarkupFormat.
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]PangoMarkupFormat of available tags in the Pango manual.)
The markup passed to 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 Label.setMarkup, 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; 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.
Labels can be made selectable with 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.
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 Label.setLineWrap.
The width-chars and 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.
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);
Sets our main struct and passes it to the parent class.
Creates a new GtkLabel, containing the text in str. If characters in str are preceded by an underscore, they are underlined. If you need a literal underscore character in a label, use '__' (two underscores). The first underlined character represents a keyboard accelerator called a mnemonic. The mnemonic key can be used to activate another widget, chosen automatically, or explicitly using setMnemonicWidget().
A [keybinding signal]GtkBindingSignal which gets emitted when the user activates a link in the label.
The signal which gets emitted to activate a URI. Applications may connect to it to override the default behaviour, which is to call gtk_show_uri_on_window().
The ::copy-clipboard signal is a [keybinding signal]GtkBindingSignal which gets emitted to copy the selection to the clipboard.
The ::move-cursor signal is a [keybinding signal]GtkBindingSignal which gets emitted when the user initiates a cursor movement. If the cursor is not visible in entry, this signal causes the viewport to be moved instead.
The ::populate-popup signal gets emitted before showing the context menu of the label. Note that only selectable labels have context menus.
Gets the angle of rotation for the label. See Label.setAngle.
Gets the attribute list that was set on the label using Label.setAttributes, if any. This function does not reflect attributes that come from the labels markup (see Label.setMarkup). If you want to get the effective attributes for the label, use pango_layout_get_attribute (gtk_label_get_layout (label)).
Returns 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.
Returns the ellipsizing position of the label. See Label.setEllipsize.
Returns the justification of the label. See Label.setJustify.
Fetches the text from a label widget including any embedded underlines indicating mnemonics and Pango markup. (See Label.getText).
Get the main Gtk struct
Gets the gtk.Layout used to display the label. The layout is useful to e.g. convert text positions to pixel positions, in combination with Label.getLayoutOffsets. The returned layout is owned by the label so need not be freed by the caller. The label is free to recreate its layout at any time, so it should be considered read-only.
Obtains the coordinates where the label will draw the gtk.Layout representing the text in the label; useful to convert mouse events into coordinates inside the gtk.Layout, e.g. to take some action if some part of the label is clicked. Of course you will need to create a gtk.EventBox to receive the events, and pack the label inside it, since labels are windowless (they return FALSE from Widget.getHasWindow). Remember when using the gtk.Layout functions you need to convert to and from pixels using PANGO_PIXELS() or PANGO_SCALE
Returns whether lines in the label are automatically wrapped. See Label.setLineWrap.
Returns line wrap mode used by the label. See Label.setLineWrapMode.
Gets the number of lines to which an ellipsized, wrapping label should be limited. See Label.setLines.
Retrieves the desired maximum width of label, in characters. See Label.setWidthChars.
If 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
Retrieves the target of the mnemonic (keyboard shortcut) of this label. See Label.setMnemonicWidget.
Gets the value set by Label.setSelectable.
Gets the selected range of characters in the label, returning TRUE if there’s a selection.
Returns whether the label is in single line mode.
the main Gtk struct as a void*
Fetches the text from a label widget, as displayed on the screen. This does not include any embedded underlines indicating mnemonics or Pango markup. (See Label.getLabel)
Returns whether the label is currently keeping track of clicked links.
Returns whether the label’s text is interpreted as marked up with the [Pango text markup language]PangoMarkupFormat. See gtk_label_set_use_markup ().
Returns whether an embedded underline in the label indicates a mnemonic. See Label.setUseUnderline.
Retrieves the desired width of label, in characters. See Label.setWidthChars.
Gets the xalign property for label.
Gets the yalign property for label.
Selects a range of characters in the label, if the label is selectable. See Label.setSelectable. If the label is not selectable, this function has no effect. If start_offset or end_offset are -1, then the end of the label will be substituted.
Sets 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, wrapped, or ellipsized.
Sets a PangoAttrList; the attributes in the list are applied to the label text.
Sets the mode used to ellipsize (add an ellipsis: "...") to the text if there is not enough space to render the entire string.
Sets the alignment of the lines in the text of the label relative to each other. GTK_JUSTIFY_LEFT is the default value when the widget is first created with Label.new. If you instead want to set the alignment of the label as a whole, use Widget.setHalign instead. Label.setJustify has no effect on labels containing only a single line.
Toggles line wrapping within the gtk.Label 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 size.
If line wrapping is on (see Label.setLineWrap) this controls how the line wrapping is done. The default is PANGO_WRAP_WORD which means wrap on word boundaries.
Sets 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 number of lines.
Parses 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.
Parses 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 by an underscore, they are underlined indicating that they represent a keyboard accelerator called a mnemonic.
Sets the desired maximum width in characters of label to n_chars.
If the label has been set so that it has an mnemonic key (using i.e. Label.setMarkupWithMnemonic, Label.setTextWithMnemonic, Label.newWithMnemonic or the “use_underline” property) the label can be associated with a widget that is the target of the mnemonic. When the label is inside a widget (like a gtk.Button or a gtk.Notebook tab) it is automatically associated with the correct widget, but sometimes (i.e. when the target is a gtk.Entry next to the label) you need to set it explicitly using this function.
The pattern of underlines you want under the existing text within the gtk.Label widget. For example if the current text of the label says “FooBarBaz” passing a pattern of “___ ___” will underline “Foo” and “Baz” but not “Bar”.
Selectable labels allow the user to select text from the label, for copy-and-paste.
Sets whether the label is in single line mode.
Sets the text within the gtk.Label widget. It overwrites any text that was there before.
Sets 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 mnemonic key can be used to activate another widget, chosen automatically, or explicitly using Label.setMnemonicWidget.
Sets whether the label should keep track of clicked links (and use a different color for them).
If true, an underline in the text indicates the next character should be used for the mnemonic accelerator key.
Sets the desired width in characters of label to n_chars.
Sets the xalign property for label.
Sets the yalign property for label.
the main Gtk struct
the main Gtk struct
Get the main Gtk struct
the main Gtk struct as a void*
Gets the X and Y alignment of the widget within its allocation. See Misc.setAlignment.
Gets the padding in the X and Y directions of the widget. See Misc.setPadding.
Sets the alignment of the widget.
Sets the amount of space to add around the widget.