Create  Edit  FrontPage  Index  Search  Changes  History  RSS  Login

Gtk::Label

class Gtk::Label

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 Gtk::Button, a Gtk::MenuItem, or a Gtk::OptionMenu.

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(text, true) or Gtk::Label.set_text(text, true).

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:

# Pressing Alt+H will activate this button
button = Gtk::Button.new
label = Gtk::Label.new("_Hello", true)
button.add(label)

There's a convenience function to create buttons with a mnemonic label already inside:

# Pressing Alt+H will activate this button
button = Gtk::Button.new("_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=:

# Pressing Alt+H will focus the entry
entry = Gtk::Entry.new
label = Gtk::Label.new("_Hello", true)
label.mnemonic_widget = 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:

label = Gtk::Label.new
label.set_markup("<small>Small text</small>")

(See complete documentation of available tags in the Pango Text Attribute Markup)

The markup passed to Gtk::Label#set_markup() must be valid; for example, literal </>/& characters must be escaped as &lt;, &gt;, and &amp;. If you pass text obtained from the user, file, or a network to Gtk::Label#set_markup(), you'll want to escape it with GLib::Markup.escape_text?(not implemented yet).

Markup strings are just a convenient way to set the Pango::AttrList on a label; Gtk::Label#set_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. 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 Gtk::Misc#set_alignment.

Class Methods

Gtk::Label.new(str = nil, mnemonic = false)
Creates a new label with the given text inside it. If mnemonic = true and 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.
  • str: The text of the label
  • mnemonic: true if mnemonic characters are underlined with an '_' in front of the mnemonic character
  • Returns: the new Gtk::Label

Instance Methods

attributes
Gets the list of style attributes to apply to the text of the label.
attributes=(attrs)
Sets a Pango::AttrList; the attributes in the list are applied to the label text. The attributes set with this method will be ignored if Gtk::Label#use_underline? or Gtk::Label#use_markup? is true.
set_attributes(attrs)
Same as Gtk::Label#attributes=.
text
Fetches the text from a label widget, as displayed on the screen. This does not include any embedded underlines indicating mnemonics or Pango markup. (See Gtk::Label#label)
  • Returns : the text in the label widget. This is the internal string used by the label, and must not be modified.
text=(str)
Sets the text within the Gtk::Label widget.
  • str: The text you want to set.
  • Returns: str
set_text(str, mnemonic = false)
Sets the text within the Gtk::Label widget. It overwrites any text that was there before. This will also clear any previously set mnemonic accelerators. If mnemonic = true and 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=.
  • str: The text you want to set.
  • mnemonic: true if mnemonic characters are underlined with an '_' in front of the mnemonic character
  • Returns: self
markup=(str)
Parses str which is marked up with the Pango text markup language, setting the label's text and attribute list based on the parse results.
set_markup(str, mnemonic = false)
Parses str which is marked up with the Pango text markup language, setting the label's text and attribute list based on the parse results. If mnemonic = true and 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=.
  • str: a markup string (see Pango Text Attribute Markup)
  • mnemonic: true if mnemonic characters are underlined with an '_' in front of the mnemonic character
  • Returns: self
pattern=(pattern)
The pattern of underlines you want under the existing text within the Gtk::Label widget. For example if the current text of the label says "FooBarBaz" passing a pattern of "___ ___" will underline "Foo" and "Baz" but not "Bar".
  • pattern: The pattern as described above.
  • Returns: pattern
set_pattern(pattern)
Same as Gtk::Label#pattern=.
  • pattern: The pattern as described above.
  • Returns: pattern
cursor_position
Gets the current position of the insertion cursor in chars.
  • Returns: the current position
justify
Returns the justification of the label. See Gtk::Label#justify=.
justify=(jtype)
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::Misc#alignment= instead. Gtk::Label#justify= has no effect on labels containing only a single line.
set_justify(jtype)
Same as Gtk::Label#justify=.
label
Fetches the text from a label widget including any embedded underlines indicating mnemonics and Pango markup. (See Gtk::Label#text).
  • Returns: the text of the label widget. This string is owned by the widget and must not be modified or freed.
label=(str)
Sets the text of the label. The label is interpreted as including embedded underlines and/or Pango markup depending on the values of Gtk::Label#use_underline? and Gtk::Label#use_markup?.
  • str: the new text to set for the label
  • Returns: str
set_label(str)
Same as Gtk::Label#label=.
  • str: the new text to set for the label
  • Returns: self
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.
layout_offsets
Obtains the coordinates where the label will draw the Pango::Layout representing the text in the label; 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. Of course you will need to create a Gtk::EventBox to receive the events, and pack the label inside it, since labels are a Gtk::Widget::NO_WINDOW widget. Remember when using the Pango::Layout functions you need to convert to and from pixels using Pango.pixels or Pango::SCALE. * x: location to store X offset of layout, or nil
  • y: location to store Y offset of layout, or nil
mnemonic_keyval
If the label has been set so that it has an mnemonic key this function returns the keyval used for the mnemonic accelerator. If there is no mnemonic set up it returns Gdk::Keyval::GDK_VoidSymbol.
mnemonic_widget
Retrieves the target of the mnemonic (keyboard shortcut) of this label. See Gtk::Label#mnemonic_widget=.
  • Returns: the target of the label's mnemonic, or nil if none has been set and the default algorithm will be used.
mnemonic_widget=(widget)
If the label has been set so that it has an mnemonic key (using i.e. Gtk::Label#set_markup, Gtk::Label#set_text, Gtk::Label.new or the "use_underline" property) the label can be associated with a widget that is the target of the mnemonic. When the label is inside a widget (like a Gtk::Button or a Gtk::Notebook tab) it is automatically associated with the correct widget, but sometimes (i.e. when the target is a Gtk::Entry next to the label) you need to set it explicitly using this function. The target widget will be accelerated by emitting "mnemonic_activate" 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.
set_mnemonic_widget(widget)
Same as Gtk::Label#mnemonic_widget=.
selectable?
Gets the value set by Gtk::Label#selectable=.
  • Returns: true if the user can copy text from the label
selectable=(setting)
Selectable labels allow the user to select text from the label, for copy-and-paste.
  • setting: true to allow selecting text in the label
  • Returns: setting
set_selectable(setting)
Same as Gtk::Label#selectable=.
  • setting: true to allow selecting text in the label
  • Returns: self
select_region(start_offset, end_offset)
Selects a range of characters in the label, if the label is selectable. See Gtk::Label#selectable=. If the label is not selectable, this method has no effect. If start_offset or end_offset are -1, then the end of the label will be substituted.
  • start_offset: start offset (in characters not bytes)
  • end_offset: end offset (in characters not bytes)
  • Returns: self
selection_bound
Gets the position of the opposite end of the selection from the cursor in chars.
  • Returns: position
selection_bounds
Gets the selected range of characters in the label, returning true if there's a selection.
  • Returns: [start, end] or nil
    • start: return location for start of selection, as a character offset
    • end: return location for end of selection, as a character offset
use_markup?
Returns whether the label's text is interpreted as marked up with the Pango text markup language. See Gtk::Label#use_markup=.
  • Returns: true if the label's text will be parsed for markup.
use_markup=(setting)
Sets whether the text of the label contains markup in Pango's text markup language. See Gtk::Labelset_markup?.
  • setting: true if the label's text should be parsed for markup
  • Returns: setting
set_use_markup(setting)
Same as Gtk::Label#use_markup=.
  • setting: true if the label's text should be parsed for markup
  • Returns: self
use_underline?
Returns whether an embedded underline in the label indicates a mnemonic. See Gtk::Label#use_underline=.
  • Returns: true whether an embedded underline in the label indicates the mnemonic accelerator keys.
use_underline=(setting)
If true, an underline in the text indicates the next character should be used for the mnemonic accelerator key.
  • setting: true if underlines in the text indicate mnemonics
  • Returns: setting
set_use_underline(setting)
Same as Gtk::Label#use_underline=.
  • setting: true if underlines in the text indicate mnemonics
  • Returns: setting
wrap?
Returns whether it toggles line wrapping within the Gtk::Label widget or not.
  • Returns: true if it toggles line wrapping.
wrap=(wrap)
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.
  • wrap: the setting
  • Returns: wrap
set_wrap(wrap)
Same as Gtk::Label#wrap=.
  • wrap: the setting
  • Returns: self
ellipsize
Returns the ellipsizing position of the label. See Gtk::Label#ellipsize=. Since 2.6
  • Returns: The preferred place to ellipsize the string, if the label does not have enough room to display the entire string, if at all
ellipsize=(mode)
Sets the mode used to ellipsize (add an ellipsis: "...") to the text if there is not enough space to render the entire string. Since 2.6
set_ellipsize(mode)
Same as Gtk::Label#ellipsize=. Since 2.6
width_chars
Retrieves the desired width of label, in characters. See Gtk::Label#width_chars=. Since 2.6
  • Returns: The desired width of the label, in characters
width_chars=(n_chars)
Sets the desired width in characters of label to n_chars. Since 2.6
  • n_chars: the new desired width(Integer), in characters.
  • Returns: n_chars
set_width_chars(n_chars)
Same as Gtk::Label#width_chars=. Since 2.6
  • n_chars: the new desired width(Integer), in characters.
  • Returns: self
max_width_chars
Retrieves the desired maximum width of label, in characters. See Gtk::Label#width_chars=. Since 2.6
  • Returns: the maximum width of the label in characters
max_width_chars=(n_chars)
Sets the desired maximum width in characters of label to n_chars. Since 2.6
  • n_chars: the new desired maximum width, in characters
  • Returns: n_chars
set_max_width_chars(n_chars)
Same as Gtk::Label#max_width_chars=. Since 2.6
  • n_chars: the new desired maximum width, in characters
  • Returns: self
single_line_mode?
Returns whether the label is in single line mode. Since 2.6
  • Returns: true when the label is in single line mode
single_line_mode=(single_line_mode)
Sets whether the label is in single line mode. Since 2.6
  • single_line_mode: true if the label should be in single line mode
  • Returns: single_line_mode
set_single_line_mode(single_line_mode)
Same as Gtk::Label#single_line_mode=. Since 2.6
  • single_line_mode: true if the label should be in single line mode
  • Returns: self
angle
Gets the angle of rotation for the label. See Gtk::Label#angle=. Since 2.6
  • Returns: Angle at which the label is rotated
angle=(angle)
Sets the angle of rotation for the label. An angle of 90 reads from from bottom to top, an angle of 270, from top to bottom. The angle setting for the label is ignored if the label is selectable, wrapped, or ellipsized. Since 2.6
  • angle: the angle(Float) that the baseline of the label makes with the horizontal, in degrees, measured counterclockwise
  • Returns: angle
set_angle(angle)
Same as Gtk::Label#angle=. Since 2.6
  • angle: the angle(Float) that the baseline of the label makes with the horizontal, in degrees, measured counterclockwise
  • Returns: self
copy_clipboard
  • Returns: self: FIXME
move_cursor
  • Returns: self: FIXME
ref_accessible
See Atk::Implementor#ref_accessible.
set_wrap_mode(wrap_mode)
Same as wrap_mode=.
  • wrap_mode: true if wrap is set, controls how linewrapping is done
  • Returns: self
wrap_mode
Gets the value if wrap is set, controls how linewrapping is done.
  • Returns: true if wrap is set, controls how linewrapping is done
wrap_mode=(wrap_mode)
Sets the value if wrap is set, controls how linewrapping is done.
  • wrap_mode: true if wrap is set, controls how linewrapping is done
  • Returns: wrap_mode
add_child
See Gtk::Buildable#add_child.
construct_child
See Gtk::Buildable#construct_child.
get_internal_child
See Gtk::Buildable#get_internal_child.
name
See Gtk::Buildable#name.
name=
See Gtk::Buildable#name=.
set_buildable_property
See Gtk::Buildable#set_buildable_property.
set_name
See Gtk::Buildable#set_name.
activate_current_link
  • Returns: self: FIXME
builder_name
See Gtk::Buildable#builder_name.
builder_name=
See Gtk::Buildable#builder_name=.
set_builder_name
See Gtk::Buildable#set_builder_name.
set_track_visited_links(track_visited_links)
Same as track_visited_links=.
  • track_visited_links: true if visited links should be tracked
  • Returns: self
track_visited_links=(track_visited_links)
Sets the value whether visited links should be tracked or not.
  • track_visited_links: true if visited links should be tracked
  • Returns: track_visited_links
track_visited_links?
Gets the value whether visited links should be tracked or not.
  • Returns: true if visited links should be tracked

Properties

angle: Float (Read/Write)
The angle that the baseline of the label makes with the horizontal, in degrees, measured counterclockwise. An angle of 90 reads from from bottom to top, an angle of 270, from top to bottom. Ignored if the label is selectable, wrapped, or ellipsized. Since 2.6
  • Allowed values: [0,360]
  • Default value: 0
attributes: Pango::AttrList (Read/Write)
A list of style attributes to apply to the text of the label
cursor-position: Integer (Read)
The current position of the insertion cursor in chars
  • Allowed values: >= 0
  • Default value: 0
ellipsize: ((<Pango::Layout::EllipsizeMode|Pango::Layout#PangoEllipsizeMode>)) (Read/Write)
The preferred place to ellipsize the string, if the label does not have enough room to display the entire string, specified as a Pango::Layout::EllisizeMode. Note that setting this property to a value other than Pango::Layout::ELLIPSIZE_NONE has the side-effect that the label requests only enough space to display the ellipsis "...". In particular, this means that ellipsizing labels don't work well in notebook tabs, unless the tab's ::tab-expand property is set to true. Other means to set a label's width are Gtk::Widget#size_request and Gtk::Label#width_chars=. Since 2.6
justify: Integer (Read/Write)
The alignment of the lines in the text of the label relative to each other. This does NOT affect the alignment of the label within its allocation. See Gtk::Misc#xalign for that
label: String (Read/Write)
The text of the label
  • Default value: nil
max-width-chars: Integer (Read/Write)
The desired maximum width of the label, in characters. If this property is set to -1, the width will be calculated automatically, otherwise the label will request space for no more than the requested number of characters. If the width-chars property is set to a positive value, then the max-width-chars property is ignored. Since 2.6
  • Allowed values: >= -1
  • Default value: -1
mnemonic-keyval: Integer (Read)
The mnemonic accelerator key for this label
  • Default value: 16777215
mnemonic-widget: Gtk::Widget (Read/Write)
The widget to be activated when the label's mnemonic key is pressed
pattern: String (Write)
A string with _ characters in positions correspond to characters in the text to underline
  • Default value: nil
selectable: true or false (Read/Write)
Whether the label text can be selected with the mouse
  • Default value: false
selection-bound: Integer (Read)
The position of the opposite end of the selection from the cursor in chars
  • Allowed values: >= 0
  • Default value: 0
single-line-mode: true or false (Read/Write)
Whether the label is in single line mode. In single line mode, the height of the label does not depend on the actual text, it is always set to ascent + descent of the font. This can be an advantage in situations where resizing the label because of text changes would be distracting, e.g. in a statusbar. Since 2.6
  • Default value: false
use-markup: true or false (Read/Write)
The text of the label includes XML markup. See Pango.parse_markup.
  • Default value: false
use-underline: true or false (Read/Write)
If set, an underline in the text indicates the next character should be used for the mnemonic accelerator key
  • Default value: false
width-chars: Integer (Read/Write)
The desired width of the label, in characters. If this property is set to -1, the width will be calculated automatically, otherwise the label will request either 3 characters or the property value, whichever is greater. If the width-chars property is set to a positive value, then the max-width-chars property is ignored. Since 2.6
  • Allowed values: >= -1
  • Default value: -1
wrap: true or false (Read/Write)
If set, wrap lines if the text becomes too wide
  • Default value: false
wrap-mode: Pango::WrapMode (Read/Write)
If wrap is set, controls how linewrapping is done
track-visited-links: true or false (Read/Write)
Whether visited links should be tracked

ChangeLog

  • 2005-03-29 Added 2.6 methods, Modified properties. - Masao
  • 2003-09-06 Added. - Masao