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
.
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 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 <, >, 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=\"<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?) : self
Creates a new label with the given text inside it.
-
.new
Initialize a new
Label
. - .new(*, accessible_role : Gtk::AccessibleRole? = nil, attributes : Pango::AttrList? = nil, can_focus : Bool? = nil, can_target : Bool? = nil, css_classes : Enumerable(String)? = nil, css_name : String? = nil, cursor : Gdk::Cursor? = nil, ellipsize : Pango::EllipsizeMode? = nil, extra_menu : Gio::MenuModel? = nil, focus_on_click : Bool? = nil, focusable : Bool? = nil, halign : Gtk::Align? = nil, has_default : Bool? = nil, has_focus : Bool? = nil, has_tooltip : Bool? = nil, height_request : Int32? = nil, hexpand : Bool? = nil, hexpand_set : Bool? = nil, justify : Gtk::Justification? = nil, label : String? = nil, layout_manager : Gtk::LayoutManager? = nil, lines : Int32? = nil, margin_bottom : Int32? = nil, margin_end : Int32? = nil, margin_start : Int32? = nil, margin_top : Int32? = nil, max_width_chars : Int32? = nil, mnemonic_keyval : UInt32? = nil, mnemonic_widget : Gtk::Widget? = nil, name : String? = nil, natural_wrap_mode : Gtk::NaturalWrapMode? = nil, opacity : Float64? = nil, overflow : Gtk::Overflow? = nil, parent : Gtk::Widget? = nil, receives_default : Bool? = nil, root : Gtk::Root? = nil, scale_factor : Int32? = nil, selectable : Bool? = nil, sensitive : Bool? = nil, single_line_mode : Bool? = nil, tabs : Pango::TabArray? = nil, tooltip_markup : String? = nil, tooltip_text : String? = nil, use_markup : Bool? = nil, use_underline : Bool? = nil, valign : Gtk::Align? = nil, vexpand : Bool? = nil, vexpand_set : Bool? = nil, visible : Bool? = nil, width_chars : Int32? = nil, width_request : Int32? = nil, wrap : Bool? = nil, wrap_mode : Pango::WrapMode? = nil, xalign : Float32? = nil, yalign : Float32? = nil)
-
.new_with_mnemonic(str : String?) : self
Creates a new
Gtk::Label
, containing the text in @str.
Class Method Summary
-
.g_type : UInt64
Returns the type id (GType) registered in GLib type system.
Instance Method Summary
- #activate_current_link_signal
- #activate_link_signal
-
#attributes : Pango::AttrList?
Gets the labels attribute list.
-
#attributes=(attrs : Pango::AttrList?) : Nil
Apply attributes to the label text.
- #copy_clipboard_signal
-
#current_uri : String?
Returns the URI for the currently active link in the label.
-
#ellipsize : Pango::EllipsizeMode
Returns the ellipsizing position of the label.
-
#ellipsize=(mode : Pango::EllipsizeMode) : Nil
Sets the mode used to ellipsizei the text.
-
#extra_menu : Gio::MenuModel?
Gets the extra menu model of @label.
-
#extra_menu=(model : Gio::MenuModel?) : Nil
Sets a menu model to add when constructing the context menu for @label.
-
#justify : Gtk::Justification
Returns the justification of the label.
-
#justify=(jtype : Gtk::Justification) : Nil
Sets the alignment of the lines in the text of the label relative to each other.
-
#label : String
Fetches the text from a label.
-
#label=(str : String) : Nil
Sets the text of the label.
-
#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 desired maximum width of @label, in characters.
-
#max_width_chars=(n_chars : Int32) : Nil
Sets the desired maximum width in characters of @label to @n_chars.
-
#mnemonic_keyval : UInt32
Return the mnemonic accelerator.
-
#mnemonic_widget : Gtk::Widget?
Retrieves the target of the mnemonic (keyboard shortcut) of this label.
-
#mnemonic_widget=(widget : Gtk::Widget?) : Nil
Associate the label with its mnemonic target.
- #move_cursor_signal
-
#natural_wrap_mode : Gtk::NaturalWrapMode
Returns line wrap mode used by the label.
-
#natural_wrap_mode=(wrap_mode : Gtk::NaturalWrapMode) : Nil
Select 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?
Gets the tabs for @self.
-
#tabs=(tabs : Pango::TabArray?) : Nil
Sets the default tab stops for paragraphs in @self.
-
#text : String
Fetches the text from a label.
-
#text=(str : String) : Nil
Sets the text within the
Gtk::Label
widget. -
#text_with_mnemonic=(str : String) : Nil
Sets the label’s text from the string @str.
-
#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 an embedded 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 @label, in characters.
-
#width_chars=(n_chars : Int32) : Nil
Sets the desired width in characters of @label to @n_chars.
-
#wrap : Bool
Returns whether lines in the label are automatically wrapped.
-
#wrap=(wrap : Bool) : Nil
Toggles line wrapping within the
Gtk::Label
widget. - #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::ConstraintTarget
to_unsafe
to_unsafe
Class methods inherited from module Gtk::ConstraintTarget
g_type : UInt64
g_type
Instance methods inherited from module Gtk::Buildable
buildable_id : String?
buildable_id,
to_unsafe
to_unsafe
Class methods inherited from module Gtk::Buildable
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
Class methods inherited from module Gtk::Accessible
g_type : UInt64
g_type
Instance methods inherited from class Gtk::Widget
action_set_enabled(action_name : String, enabled : Bool) : Nil
action_set_enabled,
activate : Bool
activate,
activate_action(name : String, args : _?) : 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
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?
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=,
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?) : 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?
cursor,
cursor=(cursor : Gdk::Cursor?) : Nil
cursor=,
cursor_from_name=(name : String?) : 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?
first_child,
focus_child : Gtk::Widget?
focus_child,
focus_child=(child : Gtk::Widget?) : 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?
font_map,
font_map=(font_map : Pango::FontMap?) : Nil
font_map=,
font_options : Cairo::FontOptions?
font_options,
font_options=(options : Cairo::FontOptions?) : Nil
font_options=,
frame_clock : Gdk::FrameClock?
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,
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
insert_action_group,
insert_after(parent : Gtk::Widget, previous_sibling : Gtk::Widget?) : Nil
insert_after,
insert_before(parent : Gtk::Widget, next_sibling : Gtk::Widget?) : 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?
last_child,
layout_manager : Gtk::LayoutManager?
layout_manager,
layout_manager=(layout_manager : Gtk::LayoutManager?) : 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?
native,
next_sibling : Gtk::Widget?
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?
parent,
parent=(parent : Gtk::Widget) : Nil
parent=,
pick(x : Float64, y : Float64, flags : Gtk::PickFlags) : Gtk::Widget?
pick,
preferred_size : Gtk::Requisition
preferred_size,
prev_sibling : Gtk::Widget?
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?
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?
tooltip_markup,
tooltip_markup=(value : String) : Stringtooltip_markup=(markup : String?) : Nil tooltip_markup=, tooltip_text : String? tooltip_text, tooltip_text=(value : String) : String
tooltip_text=(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, can_focus : Bool? = nil, can_target : Bool? = nil, css_classes : Enumerable(String)? = nil, css_name : String? = nil, cursor : Gdk::Cursor? = nil, focus_on_click : Bool? = nil, focusable : Bool? = nil, halign : Gtk::Align? = nil, has_default : Bool? = nil, has_focus : Bool? = nil, has_tooltip : Bool? = nil, height_request : Int32? = nil, hexpand : Bool? = nil, hexpand_set : Bool? = nil, layout_manager : Gtk::LayoutManager? = nil, margin_bottom : Int32? = nil, margin_end : Int32? = nil, margin_start : Int32? = nil, margin_top : Int32? = nil, name : String? = nil, opacity : Float64? = nil, overflow : Gtk::Overflow? = nil, parent : Gtk::Widget? = nil, receives_default : Bool? = nil, root : Gtk::Root? = nil, scale_factor : Int32? = nil, sensitive : Bool? = nil, tooltip_markup : String? = nil, tooltip_text : String? = nil, valign : Gtk::Align? = nil, vexpand : Bool? = nil, vexpand_set : Bool? = nil, visible : Bool? = nil, width_request : Int32? = 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
Class methods inherited from module Gtk::ConstraintTarget
g_type : UInt64
g_type
Instance methods inherited from module Gtk::Buildable
buildable_id : String?
buildable_id,
to_unsafe
to_unsafe
Class methods inherited from module Gtk::Buildable
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
Class methods inherited from module Gtk::Accessible
g_type : UInt64
g_type
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
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)?
data,
finalize
finalize,
freeze_notify : Nil
freeze_notify,
getv(names : Enumerable(String), values : Enumerable(_)) : Nil
getv,
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)?
qdata,
ref_count : UInt32
ref_count,
run_dispose : Nil
run_dispose,
set_data(key : String, data : Pointer(Void)?) : Nil
set_data,
set_property(property_name : String, value : _) : Nil
set_property,
steal_data(key : String) : Pointer(Void)?
steal_data,
steal_qdata(quark : UInt32) : Pointer(Void)?
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,
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
compat_control(what : UInt64, data : Pointer(Void)?) : 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
Constructor Detail
Creates a new label with the given text inside it.
You can pass nil
to get an empty label widget.
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.
Class Method Detail
Instance Method Detail
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))
.
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 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.
Sets the mode used to ellipsizei the text.
The text will be ellipsized 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 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.
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 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=
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 desired maximum width of @label, in characters.
Sets the desired maximum width in characters of @label to @n_chars.
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
.
Retrieves the target of the mnemonic (keyboard shortcut) of this label.
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.
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.
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.
Sets whether the label is in single line mode.
Gets the tabs for @self.
The returned array will be nil
if “standard” (8-space) tabs are used.
Free the return value with Pango::TabArray#free
.
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
)
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=
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=
.
Returns whether the label’s text is interpreted as Pango markup.
Returns whether an embedded underlines in the label indicate mnemonics.
Retrieves the desired width of @label, in characters.
Sets the desired width in characters of @label to @n_chars.
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=
.
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.