Label
Labels are the main method of placing non-editable text in windows, for instance to place a title next to an Entry
widget. You can specify the text in the constructor, or later with the Label.setText
or Label.setMarkup
methods.
The width of the label will be adjusted automatically. You can produce multi-line labels by putting line breaks (“\n
”)
in the label string.
Labels can be made selectable with 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.
The label text can be justified using the Label.justify
property. The widget is also capable of word-wrapping, which
can be activated with Label.wrap
.
Label
support some simple formatting, for instance allowing you to make some text bold, colored, or larger. You can do
this by providing a string to Label._markup
, using
the Pango Markup syntax. For
instance, <b>bold text</b> and <s>strikethrough text</s>
.
In addition, Label
supports clickable hyperlinks. The markup for links is borrowed from HTML, using the a with href
and title attributes. GTK4 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.
label.setMarkup(
"Go to <a href=\"https://www.org\" title=\"Our website\">GTK4 website</a> for more"
)
Labels may contain mnemonics. Mnemonics are underlined characters in the label, used for keyboard navigation.
Mnemonics are created by providing a string with an underscore before the mnemonic character, such as "_File"
, to the
functions Label.newWithMnemonic
or Label.setTextWithMnemonic
.
Mnemonics automatically activate any activatable widget the label is inside, such as a Button
; if the label is not
inside the mnemonic’s target widget, you have to tell the label about the target using Label.mnemonicWidget
.
Example
fun label(): Box {
val hbox = Box(Orientation.HORIZONTAL, spacing = 10).apply {
homogeneous = false
marginStart = 16
marginTop = 16
marginEnd = 16
marginBottom = 16
}
val vboxLeft = Box(orientation = Orientation.VERTICAL, spacing = 10).apply {
homogeneous = false
vexpand = true
hexpand = true
}
val vboxRight = Box(orientation = Orientation.VERTICAL, spacing = 10).apply {
homogeneous = false
vexpand = true
hexpand = true
}
hbox.append(vboxLeft)
hbox.append(vboxRight)
val button = getButton()
vboxLeft.apply {
append(getNormalLabel())
append(getLeftJustifiedLabel())
append(getRightJustifiedLabel())
append(getMarkupLabel())
append(getLabelWithMnemonic(button))
}
vboxRight.apply {
append(getLineWrappedLabel())
append(getLineWrappedFilledLabel())
append(button)
}
return hbox
}
private fun getNormalLabel() =
Label(str = "This is a normal label").apply {
vexpand = true
hexpand = true
}
private fun getLeftJustifiedLabel() = Label("").apply {
setText("This is a left-justified label.\nWith multiple lines.")
vexpand = true
hexpand = true
justify = Justification.LEFT
}
private fun getRightJustifiedLabel() = Label("This is a right-justified label.\nWith multiple lines.").apply {
vexpand = true
hexpand = true
justify = Justification.RIGHT
}
private fun getLineWrappedLabel() = Label(
str = "This is an example of a line-wrapped label. It should not be taking up the entire width " +
"allocated to it, but automatically wraps the words to fit.\n It supports multiple paragraphs correctly, " +
"and correctly adds many extra spaces. ",
).apply {
vexpand = true
hexpand = true
wrap = true
maxWidthChars = 32
}
private fun getLineWrappedFilledLabel() = Label(
str = "This is an example of a line-wrapped, filled label. It should be taking up the entire width " +
"allocated to it. Here is a sentence to prove my point. Here is another sentence. Here comes the sun, do de" +
" do de do.\n This is a new paragraph.\n This is another newer, longer, better paragraph. It is " +
"coming to an end, unfortunately.",
).apply {
vexpand = true
hexpand = true
wrap = true
justify = Justification.FILL
maxWidthChars = 32
}
private fun getMarkupLabel() = Label("").apply {
setMarkup(
"Text can be <small>small</small>, <big>big</big>, <b>bold</b>, <i>italic</i> and even point to " +
"somewhere in the <a href=\"https://www.gtk.org\" title=\"Click to find out more\">internets</a>.",
)
vexpand = true
hexpand = true
wrap = true
maxWidthChars = 48
}
private fun getLabelWithMnemonic(button: Widget) =
Label.newWithMnemonic("_Press Alt + P to select button to the right").apply {
vexpand = true
hexpand = true
selectable = true
mnemonicWidget = button
}
private fun getButton() =
Button(label = "It does nothing").apply {
vexpand = true
hexpand = true
}