class Gtk::Label

Overview

The Gtk::Label widget displays a small amount of text.

As the name implies, most labels are used to label another widget such as a #Button.

An example Gtk::Label

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

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_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#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_set_markup() must be valid; for example, literal <, > and & characters must be escaped as &lt;, >, and &. If you pass text obtained from the user, file, or a network to Gtk::Label#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#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> 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:

WARNING ⚠️ The following code is in c ⚠️

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...";
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.cr

Constructors

Class Method Summary

Instance Method Summary

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_role : Gtk::AccessibleRole accessible_role, accessible_role=(value : Gtk::AccessibleRole) : Gtk::AccessibleRole accessible_role=, reset_property(property : Gtk::AccessibleProperty) : Nil reset_property, reset_relation(relation : Gtk::AccessibleRelation) : Nil reset_relation, reset_state(state : Gtk::AccessibleState) : Nil reset_state, to_unsafe to_unsafe, 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, 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=, children : Iterator(Widget) children, clipboard : Gdk::Clipboard clipboard, 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) : String 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, 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=, 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=, 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, template_child(widget_type : UInt64, name : String) : GObject::Object
template_child(name : String) : GObject::Object
template_child
, tooltip_markup : String | Nil tooltip_markup, tooltip_markup=(value : String) : String
tooltip_markup=(markup : String | Nil) : Nil
tooltip_markup=
, tooltip_text : String | Nil tooltip_text, tooltip_text=(value : String) : String
tooltip_text=(text : String | Nil) : 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

new
new(*, 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, 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_role : Gtk::AccessibleRole accessible_role, accessible_role=(value : Gtk::AccessibleRole) : Gtk::AccessibleRole accessible_role=, reset_property(property : Gtk::AccessibleProperty) : Nil reset_property, reset_relation(relation : Gtk::AccessibleRelation) : Nil reset_relation, reset_state(state : Gtk::AccessibleState) : Nil reset_state, to_unsafe to_unsafe, 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

def self.new(str : String | Nil) : self #

Creates a new label with the given text inside it.

You can pass nil to get an empty label widget.


[View source]
def self.new #

Initialize a new Label.


[View source]
def self.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, 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, 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) #

[View source]
def self.new_with_mnemonic(str : String | Nil) : self #

Creates a new Gtk::Label, 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 Gtk::Label#mnemonic_widget=.

If Gtk::Label#mnemonic_widget= is not called, then the first activatable ancestor of the Gtk::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.


[View source]

Class Method Detail

def self.g_type : UInt64 #

Returns the type id (GType) registered in GLib type system.


[View source]

Instance Method Detail

def ==(other : self) #
Description copied from class Reference

Returns true if this reference is the same as other. Invokes same?.


def activate_current_link_signal #

[View source]
def activate_link_signal #

[View source]
def attributes : Pango::AttrList | Nil #

Gets the labels 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 labels markup (see Gtk::Label#markup=). If you want to get the effective attributes for the label, use pango_layout_get_attribute (gtk_label_get_layout (self)).


[View source]
def attributes=(attrs : Pango::AttrList | Nil) : Nil #

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.


[View source]
def copy_clipboard_signal #

[View source]
def current_uri : String | Nil #

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.

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.


[View source]
def ellipsize : Pango::EllipsizeMode #

Returns the ellipsizing position of the label.

See Gtk::Label#ellipsize=.


[View source]
def ellipsize=(mode : Pango::EllipsizeMode) : Nil #

Sets the mode used to ellipsizei the text.

The text will be ellipsized if there is not enough space to render the entire string.


[View source]
def extra_menu : Gio::MenuModel | Nil #

Gets the extra menu model of label.

See Gtk::Label#extra_menu=.


[View source]
def extra_menu=(model : Gio::MenuModel | Nil) : Nil #

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


[View source]
def hash(hasher) #
Description copied from class Reference

See Object#hash(hasher)


def justify : Gtk::Justification #

Returns the justification of the label.

See Gtk::Label#justify=.


[View source]
def justify=(jtype : Gtk::Justification) : Nil #

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 Gtk::Label.new. If you instead want to set the alignment of the label as a whole, use Gtk::Widget#halign= instead. Gtk::Label#justify= has no effect on labels containing only a single line.


[View source]
def label : String #

Fetches the text from a label.

The returned text includes any embedded underlines indicating mnemonics and Pango markup. (See Gtk::Label#text).


[View source]
def label=(str : String) : Nil #

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.


[View source]
def layout : Pango::Layout #

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.


[View source]
def layout_offsets : Nil #

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.


[View source]
def lines : Int32 #

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

See Gtk::Label#lines=.


[View source]
def lines=(lines : Int32) : Nil #

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.


[View source]
def markup=(str : String) : Nil #

Sets the labels text and attributes from markup.

The string must be marked up with Pango markup (see Pango::parse_markup).

If the str is external data, you may need to escape it with g_markup_escape_text() or g_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 will set the [property@Gtk.Label:use-markup] property to true as a side effect.

If you set the label contents using the Gtk::Label#label property you should also ensure that you set the [property@Gtk.Label:use-markup] property accordingly.

See also: Gtk::Label#text=


[View source]
def markup_with_mnemonic=(str : String) : Nil #

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


[View source]
def max_width_chars : Int32 #

Retrieves the desired maximum width of label, in characters.

See Gtk::Label#width_chars=.


[View source]
def max_width_chars=(n_chars : Int32) : Nil #

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


[View source]
def mnemonic_keyval : UInt32 #

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.


[View source]
def mnemonic_widget : Gtk::Widget | Nil #

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

See Gtk::Label#mnemonic_widget=.


[View source]
def mnemonic_widget=(widget : Gtk::Widget | Nil) : Nil #

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.


[View source]
def move_cursor_signal #

[View source]
def natural_wrap_mode : Gtk::NaturalWrapMode #

Returns line wrap mode used by the label.

See Gtk::Label#natural_wrap_mode=.


[View source]
def natural_wrap_mode=(wrap_mode : Gtk::NaturalWrapMode) : Nil #

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


[View source]
def select_region(start_offset : Int32, end_offset : Int32) : Nil #

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.


[View source]
def selectable : Bool #

Returns whether the label is selectable.


[View source]
def selectable=(setting : Bool) : Nil #

Makes text in the label selectable.

Selectable labels allow the user to select text from the label, for copy-and-paste.


[View source]
def selectable? : Bool #

[View source]
def selection_bounds : Bool #

Gets the selected range of characters in the label.


[View source]
def single_line_mode : Bool #

Returns whether the label is in single line mode.


[View source]
def single_line_mode=(single_line_mode : Bool) : Nil #

Sets whether the label is in single line mode.


[View source]
def single_line_mode? : Bool #

[View source]
def text : String #

Fetches the text from a 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)


[View source]
def text=(str : String) : Nil #

Sets the text within the Gtk::Label widget.

It overwrites any text that was there before.

This function will clear any previously set mnemonic accelerators, and set the [property@Gtk.Label:use-underline property] to false as a side effect.

This function will set the [property@Gtk.Label:use-markup] property to false as a side effect.

See also: Gtk::Label#markup=


[View source]
def text_with_mnemonic=(str : String) : Nil #

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 Gtk::Label#mnemonic_widget=.


[View source]
def use_markup : Bool #

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

See Gtk::Label#use_markup=.


[View source]
def use_markup=(setting : Bool) : Nil #

Sets whether the text of the label contains markup.

See Gtk::Label#markup=.


[View source]
def use_markup? : Bool #

[View source]
def use_underline : Bool #

Returns whether an embedded underlines in the label indicate mnemonics.

See Gtk::Label#use_underline=.


[View source]
def use_underline=(setting : Bool) : Nil #

Sets whether underlines in the text indicate mnemonics.


[View source]
def use_underline? : Bool #

[View source]
def width_chars : Int32 #

Retrieves the desired width of label, in characters.

See Gtk::Label#width_chars=.


[View source]
def width_chars=(n_chars : Int32) : Nil #

Sets the desired width in characters of label to n_chars.


[View source]
def wrap : Bool #

Returns whether lines in the label are automatically wrapped.

See Gtk::Label#wrap=.


[View source]
def wrap=(wrap : Bool) : Nil #

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.

Note that setting line wrapping to true does not make the label wrap at its parent container’s width, because GTK widgets conceptually can’t make their requisition depend on the parent container’s size. For a label that wraps at a specific position, set the label’s width using Gtk::Widget#size_request=.


[View source]
def wrap? : Bool #

[View source]
def wrap_mode : Pango::WrapMode #

Returns line wrap mode used by the label.

See Gtk::Label#wrap_mode=.


[View source]
def wrap_mode=(wrap_mode : Pango::WrapMode) : Nil #

Controls how line wrapping is done.

This only affects the label if line wrapping is on. (See Gtk::Label#wrap=) The default is %PANGO_WRAP_WORD which means wrap on word boundaries.

For sizing behavior, also consider the [property@Gtk.Label:natural-wrap-mode] property.


[View source]
def xalign : Float32 #

Gets the #xalign of the label.

See the Gtk::Label#xalign property.


[View source]
def xalign=(xalign : Float32) : Nil #

Sets the #xalign of the label.

See the Gtk::Label#xalign property.


[View source]
def yalign : Float32 #

Gets the #yalign of the label.

See the Gtk::Label#yalign property.


[View source]
def yalign=(yalign : Float32) : Nil #

Sets the #yalign of the label.

See the Gtk::Label#yalign property.


[View source]