class Gtk::Label
Overview
Displays a small amount of text.
Most labels are used to label another widget (such as an #Entry
).

Shortcuts and Gestures
Gtk::Label
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:
- [signal@Gtk.Label::activate-current-link]
- [signal@Gtk.Label::copy-clipboard]
- [signal@Gtk.Label::move-cursor]
Actions
Gtk::Label
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]
Gtk::Label
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
, 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.
Gtk::Label as Gtk::Buildable
The Gtk::Label implementation of the Gtk::Buildable 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 Pango::Attribute
values for this label.
An example of a UI definition fragment specifying Pango attributes:
WARNING ⚠️ The following code is in xml ⚠️
<object class="Gtk::Label">
<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
Gtk::Label
uses the Gtk::AccessibleRole#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#text_with_mnemonic=
.
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 Gtk::Label#mnemonic_widget=
.
Here’s a simple example where the label is inside a button:
WARNING ⚠️ The following code is in c ⚠️
// Pressing Alt+H will activate this button
Gtk::Widget *button = gtk_button_new ();
Gtk::Widget *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:
WARNING ⚠️ The following code is in c ⚠️
// Pressing Alt+H will activate this button
Gtk::Widget *button = gtk_button_new_with_mnemonic ("_Hello");
To create a mnemonic for a widget alongside the label, such as a
Gtk::Entry
, you have to point the label at the entry with
Gtk::Label#mnemonic_widget=
:
WARNING ⚠️ The following code is in c ⚠️
// Pressing Alt+H will focus the entry
Gtk::Widget *entry = gtk_entry_new ();
Gtk::Widget *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:
WARNING ⚠️ The following code is in c ⚠️
Gtk::Widget *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#markup=
must be valid XML; 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#markup=
, you’ll want to escape it with
GLib::markup_escape_text
or GLib::markup_printf_escaped
.
Markup strings are just a convenient way to set the Pango::AttrList
on a label; Gtk::Label#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#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#wrap=
.
Gtk::Label#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 Gtk::Widget#halign
and
Gtk::Widget#valign
properties.
The [property@Gtk.Label:width-chars] and [property@Gtk.Label: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>
tag
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:
WARNING ⚠️ The following code is in c ⚠️
const char *text =
"Go to the "
"<a href=\"https://www.gtk.org\" title=\"<i>Our</i> website\">"
"GTK website</a> for more...";
Gtk::Widget *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 [signal@Gtk.Label::activate-link] signal and the
Gtk::Label#current_uri
function.
Included Modules
Defined in:
lib/gi-crystal/src/auto/gtk-4.0/label.crConstructors
-
.new(str : String | Nil) : self
Creates a new label with the given text inside it.
-
.new
Initialize a new
Label
. - .new(*, accessible_role : Gtk::AccessibleRole | Nil = nil, attributes : Pango::AttrList | Nil = nil, can_focus : Bool | Nil = nil, can_target : Bool | Nil = nil, css_classes : Enumerable(String) | Nil = nil, css_name : String | Nil = nil, cursor : Gdk::Cursor | Nil = nil, ellipsize : Pango::EllipsizeMode | Nil = nil, extra_menu : Gio::MenuModel | Nil = nil, focus_on_click : Bool | Nil = nil, focusable : Bool | Nil = nil, halign : Gtk::Align | Nil = nil, has_default : Bool | Nil = nil, has_focus : Bool | Nil = nil, has_tooltip : Bool | Nil = nil, height_request : Int32 | Nil = nil, hexpand : Bool | Nil = nil, hexpand_set : Bool | Nil = nil, justify : Gtk::Justification | Nil = nil, label : String | Nil = nil, layout_manager : Gtk::LayoutManager | Nil = nil, limit_events : Bool | Nil = nil, lines : Int32 | Nil = nil, margin_bottom : Int32 | Nil = nil, margin_end : Int32 | Nil = nil, margin_start : Int32 | Nil = nil, margin_top : Int32 | Nil = nil, max_width_chars : Int32 | Nil = nil, mnemonic_keyval : UInt32 | Nil = nil, mnemonic_widget : Gtk::Widget | Nil = nil, name : String | Nil = nil, natural_wrap_mode : Gtk::NaturalWrapMode | Nil = nil, opacity : Float64 | Nil = nil, overflow : Gtk::Overflow | Nil = nil, parent : Gtk::Widget | Nil = nil, receives_default : Bool | Nil = nil, root : Gtk::Root | Nil = nil, scale_factor : Int32 | Nil = nil, selectable : Bool | Nil = nil, sensitive : Bool | Nil = nil, single_line_mode : Bool | Nil = nil, tabs : Pango::TabArray | Nil = nil, tooltip_markup : String | Nil = nil, tooltip_text : String | Nil = nil, use_markup : Bool | Nil = nil, use_underline : Bool | Nil = nil, valign : Gtk::Align | Nil = nil, vexpand : Bool | Nil = nil, vexpand_set : Bool | Nil = nil, visible : Bool | Nil = nil, width_chars : Int32 | Nil = nil, width_request : Int32 | Nil = nil, wrap : Bool | Nil = nil, wrap_mode : Pango::WrapMode | Nil = nil, xalign : Float32 | Nil = nil, yalign : Float32 | Nil = nil)
-
.new_with_mnemonic(str : String | Nil) : self
Creates a new label with the given text inside it, and a mnemonic.
Class Method Summary
-
.g_type : UInt64
Returns the type id (GType) registered in GLib type system.
Instance Method Summary
-
#==(other : self)
Returns
true
if this reference is the same as other. - #activate_current_link_signal
- #activate_link_signal
-
#attributes : Pango::AttrList | Nil
Gets the label's attribute list.
-
#attributes=(attrs : Pango::AttrList | Nil) : Nil
Apply attributes to the label text.
- #copy_clipboard_signal
-
#current_uri : String | Nil
Returns the URI for the active link in the label.
-
#ellipsize : Pango::EllipsizeMode
Returns the ellipsization mode of the label.
-
#ellipsize=(mode : Pango::EllipsizeMode) : Nil
Sets the mode used to ellipsize the text.
-
#extra_menu : Gio::MenuModel | Nil
Gets the extra menu model of the label.
-
#extra_menu=(model : Gio::MenuModel | Nil) : Nil
Sets a menu model to add to the context menu of the label.
-
#hash(hasher)
See
Object#hash(hasher)
-
#justify : Gtk::Justification
Returns the justification of the label.
-
#justify=(jtype : Gtk::Justification) : Nil
Sets the alignment of lines in the label relative to each other.
-
#label : String
Fetches the text from a label.
-
#label=(str : String) : Nil
Sets the text of the label.
-
#label=(value : Nil) : Nil
Set
#label
property to nil. -
#label? : String | Nil
Same as
#label
, but can return nil. -
#layout : Pango::Layout
Gets the Pango layout used to display the label.
-
#layout_offsets : Nil
Obtains the coordinates where the label will draw its Pango layout.
-
#lines : Int32
Gets the number of lines to which an ellipsized, wrapping label should be limited.
-
#lines=(lines : Int32) : Nil
Sets the number of lines to which an ellipsized, wrapping label should be limited.
-
#markup=(str : String) : Nil
Sets the labels text and attributes from markup.
-
#markup_with_mnemonic=(str : String) : Nil
Sets the labels text, attributes and mnemonic from markup.
-
#max_width_chars : Int32
Retrieves the maximum width of the label in characters.
-
#max_width_chars=(n_chars : Int32) : Nil
Sets the maximum width of the label in characters.
-
#mnemonic_keyval : UInt32
Return the mnemonic accelerator.
-
#mnemonic_widget : Gtk::Widget | Nil
Retrieves the mnemonic target of this label.
-
#mnemonic_widget=(widget : Gtk::Widget | Nil) : Nil
Associate the label with its mnemonic target.
- #move_cursor_signal
-
#natural_wrap_mode : Gtk::NaturalWrapMode
Returns natural line wrap mode used by the label.
-
#natural_wrap_mode=(wrap_mode : Gtk::NaturalWrapMode) : Nil
Selects the line wrapping for the natural size request.
-
#select_region(start_offset : Int32, end_offset : Int32) : Nil
Selects a range of characters in the label, if the label is selectable.
-
#selectable : Bool
Returns whether the label is selectable.
-
#selectable=(setting : Bool) : Nil
Makes text in the label selectable.
- #selectable? : Bool
-
#selection_bounds : Bool
Gets the selected range of characters in the label.
-
#single_line_mode : Bool
Returns whether the label is in single line mode.
-
#single_line_mode=(single_line_mode : Bool) : Nil
Sets whether the label is in single line mode.
- #single_line_mode? : Bool
-
#tabs : Pango::TabArray | Nil
Gets the tab stops for the label.
-
#tabs=(tabs : Pango::TabArray | Nil) : Nil
Sets tab stops for the label.
-
#text : String
Gets the text of the label.
-
#text=(str : String) : Nil
Sets the text for the label.
-
#text_with_mnemonic=(str : String) : Nil
Sets the text for the label, with mnemonics.
-
#use_markup : Bool
Returns whether the label’s text is interpreted as Pango markup.
-
#use_markup=(setting : Bool) : Nil
Sets whether the text of the label contains markup.
- #use_markup? : Bool
-
#use_underline : Bool
Returns whether underlines in the label indicate mnemonics.
-
#use_underline=(setting : Bool) : Nil
Sets whether underlines in the text indicate mnemonics.
- #use_underline? : Bool
-
#width_chars : Int32
Retrieves the desired width of the label in characters.
-
#width_chars=(n_chars : Int32) : Nil
Sets the desired width in characters of the label.
-
#wrap : Bool
Returns whether lines in the label are automatically wrapped.
-
#wrap=(wrap : Bool) : Nil
Toggles line wrapping within the label.
- #wrap? : Bool
-
#wrap_mode : Pango::WrapMode
Returns line wrap mode used by the label.
-
#wrap_mode=(wrap_mode : Pango::WrapMode) : Nil
Controls how line wrapping is done.
-
#xalign : Float32
Gets the
#xalign
of the label. -
#xalign=(xalign : Float32) : Nil
Sets the
#xalign
of the label. -
#yalign : Float32
Gets the
#yalign
of the label. -
#yalign=(yalign : Float32) : Nil
Sets the
#yalign
of the label.
Instance methods inherited from module Gtk::AccessibleText
to_unsafe
to_unsafe,
update_caret_position : Nil
update_caret_position,
update_contents(change : Gtk::AccessibleTextContentChange, start : UInt32, end _end : UInt32) : Nil
update_contents,
update_selection_bound : Nil
update_selection_bound
Constructor methods inherited from module Gtk::AccessibleText
cast(obj : GObject::Object) : self
cast
Class methods inherited from module Gtk::AccessibleText
cast?(obj : GObject::Object) : self | Nil
cast?,
g_type : UInt64
g_type
Instance methods inherited from module Gtk::ConstraintTarget
to_unsafe
to_unsafe
Constructor methods inherited from module Gtk::ConstraintTarget
cast(obj : GObject::Object) : self
cast
Class methods inherited from module Gtk::ConstraintTarget
cast?(obj : GObject::Object) : self | Nil
cast?,
g_type : UInt64
g_type
Instance methods inherited from module Gtk::Buildable
buildable_id : String | Nil
buildable_id,
to_unsafe
to_unsafe
Constructor methods inherited from module Gtk::Buildable
cast(obj : GObject::Object) : self
cast
Class methods inherited from module Gtk::Buildable
cast?(obj : GObject::Object) : self | Nil
cast?,
g_type : UInt64
g_type
Instance methods inherited from module Gtk::Accessible
accessible_parent : Gtk::Accessible | Nil
accessible_parent,
accessible_role : Gtk::AccessibleRole
accessible_role,
accessible_role=(value : Gtk::AccessibleRole) : Gtk::AccessibleRole
accessible_role=,
announce(message : String, priority : Gtk::AccessibleAnnouncementPriority) : Nil
announce,
at_context : Gtk::ATContext
at_context,
bounds(x : Int32, y : Int32, width : Int32, height : Int32) : Bool
bounds,
first_accessible_child : Gtk::Accessible | Nil
first_accessible_child,
next_accessible_sibling : Gtk::Accessible | Nil
next_accessible_sibling,
platform_state(state : Gtk::AccessiblePlatformState) : Bool
platform_state,
reset_property(property : Gtk::AccessibleProperty) : Nil
reset_property,
reset_relation(relation : Gtk::AccessibleRelation) : Nil
reset_relation,
reset_state(state : Gtk::AccessibleState) : Nil
reset_state,
set_accessible_parent(parent : Gtk::Accessible | Nil, next_sibling : Gtk::Accessible | Nil) : Nil
set_accessible_parent,
to_unsafe
to_unsafe,
update_next_accessible_sibling(new_sibling : Gtk::Accessible | Nil) : Nil
update_next_accessible_sibling,
update_platform_state(state : Gtk::AccessiblePlatformState) : Nil
update_platform_state,
update_property(properties : Enumerable(Gtk::AccessibleProperty), values : Enumerable(_)) : Nil
update_property,
update_relation(relations : Enumerable(Gtk::AccessibleRelation), values : Enumerable(_)) : Nil
update_relation,
update_state(states : Enumerable(Gtk::AccessibleState), values : Enumerable(_)) : Nil
update_state
Constructor methods inherited from module Gtk::Accessible
cast(obj : GObject::Object) : self
cast
Class methods inherited from module Gtk::Accessible
cast?(obj : GObject::Object) : self | Nil
cast?,
g_type : UInt64
g_type
Instance methods inherited from class Gtk::Widget
==(other : self)
==,
action_set_enabled(action_name : String, enabled : Bool) : Nil
action_set_enabled,
activate : Bool
activate,
activate_action(name : String, args : _ | Nil) : Bool
activate_action,
activate_default : Nil
activate_default,
add_controller(controller : Gtk::EventController) : Nil
add_controller,
add_css_class(css_class : String) : Nil
add_css_class,
add_mnemonic_label(label : Gtk::Widget) : Nil
add_mnemonic_label,
add_tick_callback(callback : Gtk::TickCallback) : UInt32
add_tick_callback,
allocate(width : Int32, height : Int32, baseline : Int32, transform : Gsk::Transform | Nil) : Nil
allocate,
allocated_baseline : Int32
allocated_baseline,
allocated_height : Int32
allocated_height,
allocated_width : Int32
allocated_width,
allocation : Gdk::Rectangle
allocation,
ancestor(widget_type : UInt64) : Gtk::Widget | Nil
ancestor,
baseline : Int32
baseline,
can_focus : Bool
can_focus,
can_focus=(can_focus : Bool) : Nil
can_focus=,
can_focus? : Bool
can_focus?,
can_target : Bool
can_target,
can_target=(can_target : Bool) : Nil
can_target=,
can_target? : Bool
can_target?,
child_focus(direction : Gtk::DirectionType) : Bool
child_focus,
child_visible : Bool
child_visible,
child_visible=(child_visible : Bool) : Nil
child_visible=,
clipboard : Gdk::Clipboard
clipboard,
color : Gdk::RGBA
color,
compute_bounds(target : Gtk::Widget) : Graphene::Rect
compute_bounds,
compute_expand(orientation : Gtk::Orientation) : Bool
compute_expand,
compute_point(target : Gtk::Widget, point : Graphene::Point) : Graphene::Point
compute_point,
compute_transform(target : Gtk::Widget) : Graphene::Matrix
compute_transform,
contains(x : Float64, y : Float64) : Bool
contains,
create_pango_context : Pango::Context
create_pango_context,
create_pango_layout(text : String | Nil) : Pango::Layout
create_pango_layout,
css_classes : Enumerable(String)
css_classes,
css_classes=(classes : Enumerable(String)) : Nil
css_classes=,
css_name : String
css_name,
css_name=(value : String) : Stringcss_name=(value : Nil) : Nil css_name=, css_name? : String | Nil css_name?, cursor : Gdk::Cursor | Nil cursor, cursor=(cursor : Gdk::Cursor | Nil) : Nil cursor=, cursor_from_name=(name : String | Nil) : Nil cursor_from_name=, destroy_signal destroy_signal, direction : Gtk::TextDirection direction, direction=(dir : Gtk::TextDirection) : Nil direction=, direction_changed_signal direction_changed_signal, display : Gdk::Display display, dispose_template(widget_type : UInt64) : Nil dispose_template, drag_check_threshold(start_x : Int32, start_y : Int32, current_x : Int32, current_y : Int32) : Bool drag_check_threshold, error_bell : Nil error_bell, first_child : Gtk::Widget | Nil first_child, focus_child : Gtk::Widget | Nil focus_child, focus_child=(child : Gtk::Widget | Nil) : Nil focus_child=, focus_on_click : Bool focus_on_click, focus_on_click=(focus_on_click : Bool) : Nil focus_on_click=, focus_on_click? : Bool focus_on_click?, focusable : Bool focusable, focusable=(focusable : Bool) : Nil focusable=, focusable? : Bool focusable?, font_map : Pango::FontMap | Nil font_map, font_map=(font_map : Pango::FontMap | Nil) : Nil font_map=, font_options : Cairo::FontOptions | Nil font_options, font_options=(options : Cairo::FontOptions | Nil) : Nil font_options=, frame_clock : Gdk::FrameClock | Nil frame_clock, grab_focus : Bool grab_focus, halign : Gtk::Align halign, halign=(align : Gtk::Align) : Nil halign=, has_css_class(css_class : String) : Bool has_css_class, has_default : Bool has_default, has_default? : Bool has_default?, has_focus : Bool has_focus, has_focus? : Bool has_focus?, has_tooltip : Bool has_tooltip, has_tooltip=(has_tooltip : Bool) : Nil has_tooltip=, has_tooltip? : Bool has_tooltip?, has_visible_focus : Bool has_visible_focus, hash(hasher) hash, height : Int32 height, height_request : Int32 height_request, height_request=(value : Int32) : Int32 height_request=, hexpand : Bool hexpand, hexpand=(expand : Bool) : Nil hexpand=, hexpand? : Bool hexpand?, hexpand_set : Bool hexpand_set, hexpand_set=(set : Bool) : Nil hexpand_set=, hexpand_set? : Bool hexpand_set?, hide : Nil hide, hide_signal hide_signal, in_destruction : Bool in_destruction, init_template : Nil init_template, insert_action_group(name : String, group : Gio::ActionGroup | Nil) : Nil insert_action_group, insert_after(parent : Gtk::Widget, previous_sibling : Gtk::Widget | Nil) : Nil insert_after, insert_before(parent : Gtk::Widget, next_sibling : Gtk::Widget | Nil) : Nil insert_before, is_ancestor(ancestor : Gtk::Widget) : Bool is_ancestor, is_drawable : Bool is_drawable, is_focus : Bool is_focus, is_sensitive? : Bool is_sensitive?, is_visible? : Bool is_visible?, keynav_failed(direction : Gtk::DirectionType) : Bool keynav_failed, keynav_failed_signal keynav_failed_signal, last_child : Gtk::Widget | Nil last_child, layout_manager : Gtk::LayoutManager | Nil layout_manager, layout_manager=(layout_manager : Gtk::LayoutManager | Nil) : Nil layout_manager=, limit_events : Bool limit_events, limit_events=(limit_events : Bool) : Nil limit_events=, limit_events? : Bool limit_events?, list_mnemonic_labels : GLib::List list_mnemonic_labels, map : Nil map, map_signal map_signal, mapped : Bool mapped, margin_bottom : Int32 margin_bottom, margin_bottom=(margin : Int32) : Nil margin_bottom=, margin_end : Int32 margin_end, margin_end=(margin : Int32) : Nil margin_end=, margin_start : Int32 margin_start, margin_start=(margin : Int32) : Nil margin_start=, margin_top : Int32 margin_top, margin_top=(margin : Int32) : Nil margin_top=, measure(orientation : Gtk::Orientation, for_size : Int32) : Nil measure, mnemonic_activate(group_cycling : Bool) : Bool mnemonic_activate, mnemonic_activate_signal mnemonic_activate_signal, move_focus_signal move_focus_signal, name : String name, name=(name : String) : Nil
name=(value : Nil) : Nil name=, name? : String | Nil name?, native : Gtk::Native | Nil native, next_sibling : Gtk::Widget | Nil next_sibling, observe_children : Gio::ListModel observe_children, observe_controllers : Gio::ListModel observe_controllers, opacity : Float64 opacity, opacity=(opacity : Float64) : Nil opacity=, overflow : Gtk::Overflow overflow, overflow=(overflow : Gtk::Overflow) : Nil overflow=, pango_context : Pango::Context pango_context, parent : Gtk::Widget | Nil parent, parent=(parent : Gtk::Widget) : Nil parent=, pick(x : Float64, y : Float64, flags : Gtk::PickFlags) : Gtk::Widget | Nil pick, preferred_size : Gtk::Requisition preferred_size, prev_sibling : Gtk::Widget | Nil prev_sibling, primary_clipboard : Gdk::Clipboard primary_clipboard, query_tooltip_signal query_tooltip_signal, queue_allocate : Nil queue_allocate, queue_draw : Nil queue_draw, queue_resize : Nil queue_resize, realize : Nil realize, realize_signal realize_signal, realized : Bool realized, receives_default : Bool receives_default, receives_default=(receives_default : Bool) : Nil receives_default=, receives_default? : Bool receives_default?, remove_controller(controller : Gtk::EventController) : Nil remove_controller, remove_css_class(css_class : String) : Nil remove_css_class, remove_mnemonic_label(label : Gtk::Widget) : Nil remove_mnemonic_label, remove_tick_callback(id : UInt32) : Nil remove_tick_callback, request_mode : Gtk::SizeRequestMode request_mode, root : Gtk::Root | Nil root, scale_factor : Int32 scale_factor, sensitive : Bool sensitive, sensitive=(sensitive : Bool) : Nil sensitive=, sensitive? : Bool sensitive?, set_size_request(width : Int32, height : Int32) : Nil set_size_request, set_state_flags(flags : Gtk::StateFlags, clear : Bool) : Nil set_state_flags, settings : Gtk::Settings settings, should_layout : Bool should_layout, show : Nil show, show_signal show_signal, size(orientation : Gtk::Orientation) : Int32 size, size_allocate(allocation : Gdk::Rectangle, baseline : Int32) : Nil size_allocate, size_request : Nil size_request, snapshot_child(child : Gtk::Widget, snapshot : Gtk::Snapshot) : Nil snapshot_child, state_flags : Gtk::StateFlags state_flags, state_flags_changed_signal state_flags_changed_signal, style_context : Gtk::StyleContext style_context, tooltip_markup : String | Nil tooltip_markup, tooltip_markup=(value : String) : String
tooltip_markup=(value : Nil) : Nil
tooltip_markup=(markup : String | Nil) : Nil tooltip_markup=, tooltip_markup? : String | Nil tooltip_markup?, tooltip_text : String | Nil tooltip_text, tooltip_text=(value : String) : String
tooltip_text=(value : Nil) : Nil
tooltip_text=(text : String | Nil) : Nil tooltip_text=, tooltip_text? : String | Nil tooltip_text?, translate_coordinates(dest_widget : Gtk::Widget, src_x : Float64, src_y : Float64) : Bool translate_coordinates, trigger_tooltip_query : Nil trigger_tooltip_query, unmap : Nil unmap, unmap_signal unmap_signal, unparent : Nil unparent, unrealize : Nil unrealize, unrealize_signal unrealize_signal, unset_state_flags(flags : Gtk::StateFlags) : Nil unset_state_flags, valign : Gtk::Align valign, valign=(align : Gtk::Align) : Nil valign=, vexpand : Bool vexpand, vexpand=(expand : Bool) : Nil vexpand=, vexpand? : Bool vexpand?, vexpand_set : Bool vexpand_set, vexpand_set=(set : Bool) : Nil vexpand_set=, vexpand_set? : Bool vexpand_set?, visible : Bool visible, visible=(visible : Bool) : Nil visible=, visible? : Bool visible?, width : Int32 width, width_request : Int32 width_request, width_request=(value : Int32) : Int32 width_request=
Constructor methods inherited from class Gtk::Widget
newnew(*, accessible_role : Gtk::AccessibleRole | Nil = nil, can_focus : Bool | Nil = nil, can_target : Bool | Nil = nil, css_classes : Enumerable(String) | Nil = nil, css_name : String | Nil = nil, cursor : Gdk::Cursor | Nil = nil, focus_on_click : Bool | Nil = nil, focusable : Bool | Nil = nil, halign : Gtk::Align | Nil = nil, has_default : Bool | Nil = nil, has_focus : Bool | Nil = nil, has_tooltip : Bool | Nil = nil, height_request : Int32 | Nil = nil, hexpand : Bool | Nil = nil, hexpand_set : Bool | Nil = nil, layout_manager : Gtk::LayoutManager | Nil = nil, limit_events : Bool | Nil = nil, margin_bottom : Int32 | Nil = nil, margin_end : Int32 | Nil = nil, margin_start : Int32 | Nil = nil, margin_top : Int32 | Nil = nil, name : String | Nil = nil, opacity : Float64 | Nil = nil, overflow : Gtk::Overflow | Nil = nil, parent : Gtk::Widget | Nil = nil, receives_default : Bool | Nil = nil, root : Gtk::Root | Nil = nil, scale_factor : Int32 | Nil = nil, sensitive : Bool | Nil = nil, tooltip_markup : String | Nil = nil, tooltip_text : String | Nil = nil, valign : Gtk::Align | Nil = nil, vexpand : Bool | Nil = nil, vexpand_set : Bool | Nil = nil, visible : Bool | Nil = nil, width_request : Int32 | Nil = nil) new
Class methods inherited from class Gtk::Widget
default_direction : Gtk::TextDirection
default_direction,
default_direction=(dir : Gtk::TextDirection) : Nil
default_direction=,
g_type : UInt64
g_type
Instance methods inherited from module Gtk::ConstraintTarget
to_unsafe
to_unsafe
Constructor methods inherited from module Gtk::ConstraintTarget
cast(obj : GObject::Object) : self
cast
Class methods inherited from module Gtk::ConstraintTarget
cast?(obj : GObject::Object) : self | Nil
cast?,
g_type : UInt64
g_type
Instance methods inherited from module Gtk::Buildable
buildable_id : String | Nil
buildable_id,
to_unsafe
to_unsafe
Constructor methods inherited from module Gtk::Buildable
cast(obj : GObject::Object) : self
cast
Class methods inherited from module Gtk::Buildable
cast?(obj : GObject::Object) : self | Nil
cast?,
g_type : UInt64
g_type
Instance methods inherited from module Gtk::Accessible
accessible_parent : Gtk::Accessible | Nil
accessible_parent,
accessible_role : Gtk::AccessibleRole
accessible_role,
accessible_role=(value : Gtk::AccessibleRole) : Gtk::AccessibleRole
accessible_role=,
announce(message : String, priority : Gtk::AccessibleAnnouncementPriority) : Nil
announce,
at_context : Gtk::ATContext
at_context,
bounds(x : Int32, y : Int32, width : Int32, height : Int32) : Bool
bounds,
first_accessible_child : Gtk::Accessible | Nil
first_accessible_child,
next_accessible_sibling : Gtk::Accessible | Nil
next_accessible_sibling,
platform_state(state : Gtk::AccessiblePlatformState) : Bool
platform_state,
reset_property(property : Gtk::AccessibleProperty) : Nil
reset_property,
reset_relation(relation : Gtk::AccessibleRelation) : Nil
reset_relation,
reset_state(state : Gtk::AccessibleState) : Nil
reset_state,
set_accessible_parent(parent : Gtk::Accessible | Nil, next_sibling : Gtk::Accessible | Nil) : Nil
set_accessible_parent,
to_unsafe
to_unsafe,
update_next_accessible_sibling(new_sibling : Gtk::Accessible | Nil) : Nil
update_next_accessible_sibling,
update_platform_state(state : Gtk::AccessiblePlatformState) : Nil
update_platform_state,
update_property(properties : Enumerable(Gtk::AccessibleProperty), values : Enumerable(_)) : Nil
update_property,
update_relation(relations : Enumerable(Gtk::AccessibleRelation), values : Enumerable(_)) : Nil
update_relation,
update_state(states : Enumerable(Gtk::AccessibleState), values : Enumerable(_)) : Nil
update_state
Constructor methods inherited from module Gtk::Accessible
cast(obj : GObject::Object) : self
cast
Class methods inherited from module Gtk::Accessible
cast?(obj : GObject::Object) : self | Nil
cast?,
g_type : UInt64
g_type
Instance methods inherited from class GObject::InitiallyUnowned
==(other : self)
==,
hash(hasher)
hash
Constructor methods inherited from class GObject::InitiallyUnowned
new
new
Class methods inherited from class GObject::InitiallyUnowned
g_type : UInt64
g_type
Instance methods inherited from class GObject::Object
==(other : self)
==,
bind_property(source_property : String, target : GObject::Object, target_property : String, flags : GObject::BindingFlags) : GObject::Binding
bind_property,
bind_property_full(source_property : String, target : GObject::Object, target_property : String, flags : GObject::BindingFlags, transform_to : GObject::Closure, transform_from : GObject::Closure) : GObject::Binding
bind_property_full,
data(key : String) : Pointer(Void) | Nil
data,
finalize
finalize,
freeze_notify : Nil
freeze_notify,
getv(names : Enumerable(String), values : Enumerable(_)) : Nil
getv,
hash(hasher)
hash,
notify(property_name : String) : Nil
notify,
notify_by_pspec(pspec : GObject::ParamSpec) : Nil
notify_by_pspec,
notify_signal
notify_signal,
property(property_name : String, value : _) : Nil
property,
qdata(quark : UInt32) : Pointer(Void) | Nil
qdata,
ref_count : UInt32
ref_count,
run_dispose : Nil
run_dispose,
set_data(key : String, data : Pointer(Void) | Nil) : Nil
set_data,
set_property(property_name : String, value : _) : Nil
set_property,
steal_data(key : String) : Pointer(Void) | Nil
steal_data,
steal_qdata(quark : UInt32) : Pointer(Void) | Nil
steal_qdata,
thaw_notify : Nil
thaw_notify,
to_unsafe : Pointer(Void)
to_unsafe,
watch_closure(closure : GObject::Closure) : Nil
watch_closure
Constructor methods inherited from class GObject::Object
cast(obj : GObject::Object) : self
cast,
new(pointer : Pointer(Void), transfer : GICrystal::Transfer)new new, newv(object_type : UInt64, parameters : Enumerable(GObject::Parameter)) : self newv
Class methods inherited from class GObject::Object
cast?(obj : GObject::Object) : self | Nil
cast?,
compat_control(what : UInt64, data : Pointer(Void) | Nil) : UInt64
compat_control,
g_type : UInt64
g_type,
interface_find_property(g_iface : GObject::TypeInterface, property_name : String) : GObject::ParamSpec
interface_find_property,
interface_list_properties(g_iface : GObject::TypeInterface) : Enumerable(GObject::ParamSpec)
interface_list_properties
Macros inherited from class GObject::Object
previous_vfunc(*args)
previous_vfunc,
previous_vfunc!(*args)
previous_vfunc!,
signal(signature)
signal
Constructor Detail
Creates a new label with the given text inside it.
You can pass NULL
to get an empty label widget.
Creates a new label with the given text inside it, and a mnemonic.
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
Gtk::Label#mnemonic_widget=
.
If Gtk::Label#mnemonic_widget=
is not called, then the first
activatable ancestor of the label will be chosen as the mnemonic
widget. For instance, if the label is inside a button or menu item,
the button or menu item will automatically become the mnemonic widget
and be activated by the mnemonic.
Class Method Detail
Instance Method Detail
Returns true
if this reference is the same as other. Invokes same?
.
Gets the label's attribute list.
This is the Pango::AttrList
that was set on the label using
Gtk::Label#attributes=
, if any. This function does not
reflect attributes that come from the label's markup (see
Gtk::Label#markup=
). If you want to get the effective
attributes for the label, use
pango_layout_get_attributes (gtk_label_get_layout (self))
.
Apply attributes to the label text.
The attributes set with this function will be applied and merged with any other attributes previously effected by way of the [property@Gtk.Label:use-underline] or [property@Gtk.Label:use-markup] properties
While it is not recommended to mix markup strings with manually set attributes, if you must; know that the attributes will be applied to the label after the markup string is parsed.
Returns the URI for the 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.
This function is intended for use in a [signal@Gtk.Label::activate-link] handler or for use in a [signal@Gtk.Widget::query-tooltip] handler.
Sets the mode used to ellipsize the text.
The text will be ellipsized if there is not enough space to render the entire string.
Sets the alignment of lines in the label relative to each other.
This function has no effect on labels containing only a single line.
Gtk::Justification#left
is the default value when the widget
is first created with Gtk::Label.new
.
If you instead want to set the alignment of the label as a whole,
use Gtk::Widget#halign=
instead.
Fetches the text from a label.
The returned text includes any embedded underlines indicating
mnemonics and Pango markup. (See Gtk::Label#text
).
Sets the text of the label.
The label is interpreted as including embedded underlines and/or Pango markup depending on the values of the [property@Gtk.Label:use-underline] and [property@Gtk.Label:use-markup] properties.
Gets the Pango layout used to display the label.
The layout is useful to e.g. convert text positions to pixel
positions, in combination with Gtk::Label#layout_offsets
.
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 its Pango layout.
The coordinates are useful to convert mouse events into coordinates
inside the Pango::Layout
, e.g. to take some action if some part
of the label is clicked. Remember when using the Pango::Layout
functions you need to convert to and from pixels using PANGO_PIXELS()
or Pango::SCALE
.
Gets the number of lines to which an ellipsized, wrapping label should be limited.
See Gtk::Label#lines=
.
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.
Sets the labels text and attributes from markup.
The string must be marked up with Pango markup
(see Pango::parse_markup
).
If str is external data, you may need to escape it
with GLib::markup_escape_text
or GLib::markup_printf_escaped
:
WARNING ⚠️ The following code is in c ⚠️
Gtk::Widget *self = gtk_label_new (NULL);
const char *str = "...";
const char *format = "<span style=\"italic\">\%s</span>";
char *markup;
markup = g_markup_printf_escaped (format, str);
gtk_label_set_markup (GTK_LABEL (self), markup);
g_free (markup);
This function sets the [property@Gtk.Label:use-markup] property to true.
Also see Gtk::Label#text=
.
Sets the labels text, attributes and mnemonic from markup.
Parses str which is marked up with Pango markup (see Pango::parse_markup
),
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.
The mnemonic key can be used to activate another widget, chosen
automatically, or explicitly using Gtk::Label#mnemonic_widget=
.
Retrieves the maximum width of the label in characters.
Return the mnemonic accelerator.
If the label has been set so that it has a mnemonic key this function
returns the keyval used for the mnemonic accelerator. If there is no
mnemonic set up it returns GDK_KEY_VoidSymbol
.
Associate the label with its mnemonic target.
If the label has been set so that it has a mnemonic key (using
i.e. Gtk::Label#markup_with_mnemonic=
,
Gtk::Label#text_with_mnemonic=
,
Gtk::Label#new_with_mnemonic
or the Gtk::Label#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 target widget will be accelerated by emitting the [signal@Gtk.Widget::mnemonic-activate] signal on it. The default handler for this signal will activate the widget if there are no mnemonic collisions and toggle focus between the colliding widgets otherwise.
Selects the line wrapping for the natural size request.
This only affects the natural size requested, for the actual wrapping used, see the [property@Gtk.Label:wrap-mode] property.
Selects a range of characters in the label, if the label is selectable.
See Gtk::Label#selectable=
. 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.
Makes text in the label selectable.
Selectable labels allow the user to select text from the label, for copy-and-paste.
Gets the selected range of characters in the label.
The returned start and end positions are in characters.
Sets whether the label is in single line mode.
Gets the tab stops for the label.
The returned array will be NULL
if “standard” (8-space) tabs are used.
Gets the text of the label.
The returned text is as it appears on screen. This does not include
any embedded underlines indicating mnemonics or Pango markup. (See
Gtk::Label#label
)
Sets the text for the label.
It overwrites any text that was there before and clears any previously set mnemonic accelerators, and sets the [property@Gtk.Label:use-underline] and [property@Gtk.Label:use-markup] properties to false.
Also see Gtk::Label#markup=
.
Sets the text for the label, with mnemonics.
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 Gtk::Label#mnemonic_widget=
.
Returns whether the label’s text is interpreted as Pango markup.
Returns whether underlines in the label indicate mnemonics.
Retrieves the desired width of the label in characters.
Toggles line wrapping within the label.
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.
Note that setting line wrapping to true does not make the label
wrap at its parent widget’s width, because GTK widgets conceptually
can’t make their requisition depend on the parent widget’s size.
For a label that wraps at a specific position, set the label’s width
using Gtk::Widget#size_request=
.
Controls how line wrapping is done.
This only affects the label if line wrapping is on. (See
Gtk::Label#wrap=
)
The default is Pango::WrapMode#word
, which means
wrap on word boundaries.
For sizing behavior, also consider the [property@Gtk.Label:natural-wrap-mode] property.