public class Label extends Misc
Labels are the backbone of any Window. They are frequently used to identify other controls with names, as headings in Windows, and are the building blocks that Menus and Buttons are made up of. All the difficult parts about rendering text are taken care of here, such as text direction, fonts. And, of course, you can enable them to allow their text to be copied.
Labels can display normal text or text that has been formatted with Pango markup. FIXME with a reference to our Pango guide page.
Although you can pack multiple lines into a Label, there does come a point
when the amount of text you're trying to show gets out of hand. At that
point you might want to investigate the TextView
Widget.
Widget.ButtonPressEvent, Widget.ButtonReleaseEvent, Widget.Destroy, Widget.Draw, Widget.EnterNotifyEvent, Widget.FocusInEvent, Widget.FocusOutEvent, Widget.Hide, Widget.KeyPressEvent, Widget.KeyReleaseEvent, Widget.LeaveNotifyEvent, Widget.MapEvent, Widget.MotionNotifyEvent, Widget.PopupMenu, Widget.QueryTooltip, Widget.ScrollEvent, Widget.SizeAllocate, Widget.UnmapEvent, Widget.VisibilityNotifyEvent
Constructor and Description |
---|
Label()
Create an empty Label.
|
Label(String text)
Create a Label with the specified text.
|
Modifier and Type | Method and Description |
---|---|
double |
getAngle()
Get the current angle of the Label.
|
EllipsizeMode |
getEllipsizeMode()
Get the current ellipsization used.
|
Justification |
getJustify()
Gets the justification of the text within the Label.
|
String |
getLabel()
Get the text showing in the Label, including any characters which
indicate Pango markup syntax and embedded mnemonic underline characters
that may be present.
|
String |
getText()
Get the text showing in the Label, but with any Pango markup stripped
away.
|
boolean |
getUseMarkup()
Get the current setting indicating whether the the label text is to be
interpreted as marked up with Pango's text markup language.
|
void |
selectRegion(int start,
int end)
Select a region of the text in this Label.
|
void |
setAngle(double angle)
Sets the angle of rotation for the Label.
|
void |
setEllipsize(EllipsizeMode setting)
Set the
ellipsization mode to use to make the
text fits the label width. |
void |
setJustify(Justification setting)
Sets the justification of the text within the Label.
|
void |
setLabel(String text)
Set the text showing in the Label.
|
void |
setLineWrap(boolean setting)
Set whether this Label will attempt to wrap the text provided.
|
void |
setMaxWidthChars(int width)
Set a maximum width for the Label, in characters.
|
void |
setSelectable(boolean setting)
Whether the text in this Label can be selected by the user.
|
void |
setUseMarkup(boolean setting)
Set whether the text showing in the Label is to be parsed as containing
markup in Pango's text markup language.
|
void |
setUseUnderline(boolean setting)
Set whether underscores will be interpreted as signifying that the next
character should be drawn with an underline, thereby creating the
visual effect of a mnemonic.
|
void |
setWidthChars(int width)
Explicitly set the width of the Label, in characters.
|
getAlignmentX, getAlignmentY, getPaddingX, getPaddingY, setAlignment, setPadding
activate, addEvents, connect, connect, connect, connect, connect, connect, connect, connect, connect, connect, connect, connect, connect, connect, connect, connect, connect, connect, connect, destroy, getAllocatedHeight, getAllocatedWidth, getAllocation, getCanDefault, getCanFocus, getHasFocus, getName, getParent, getPreferredHeightForWidthMinimum, getPreferredHeightForWidthNatural, getPreferredHeightMinimum, getPreferredHeightNatural, getPreferredWidthForHeightMinimum, getPreferredWidthForHeightNatural, getPreferredWidthMinimum, getPreferredWidthNatural, getRequestMode, getRequisition, getSensitive, getStyleContext, getToplevel, getWindow, grabAdd, grabDefault, grabFocus, grabRemove, hide, isSensitive, overrideBackground, overrideColor, overrideFont, queueDraw, queueDrawArea, realize, setAlignHorizontal, setAlignVertical, setCanDefault, setCanFocus, setEvents, setExpandHorizontal, setExpandVertical, setName, setSensitive, setSizeRequest, setTooltipMarkup, setTooltipText, show, showAll
public Label()
setLabel()
to place some text it.public Label(String text)
setUseMarkup(true)
.text
- the text you wish on the Label.public double getAngle()
public EllipsizeMode getEllipsizeMode()
public Justification getJustify()
public String getLabel()
getText()
which
returns the text unadorned.public String getText()
getLabel()
.public boolean getUseMarkup()
public void selectRegion(int start, int end)
start
up to but not including end
will
be selected. This assumes that the Label has been made selectable with
setSelectable(true)
.end
- If negative, then the selection will be from
start
to the end of the text in the Label.public void setAngle(double angle)
angle
- The angle that the baseline of the Label's text makes with
the horizontal. The valid range (as you'd expect) is from
0° through 360°.public void setEllipsize(EllipsizeMode setting)
ellipsization mode
to use to make the
text fits the label width.public void setJustify(Justification setting)
LEFT
. Note that this has no effect on Labels
only containing a single line of text.
If you're trying to control the positioning of the Label within its
parent, see Misc's setAlignment()
.
public void setLabel(String text)
setUseMarkup(true)
, then any markup
included in text will be interpreted as such.
WARNING:
If you have enabled use-markup (as is often the case),
beware that you must escape whatever you to this method (like you would
for feeding user data to any other XML or HTML like target). This
specifically means you need to convert &
into
&
and likewise for angle brackets. Use
Glib.markupEscapeText()
.
public void setLineWrap(boolean setting)
setSizeRequest()
.
The default is false
.
public void setMaxWidthChars(int width)
width
but not
to expand beyond that point. This is useful in conjunction with turning
setEllipsize()
on.
This sets the max-width-chars property.
Warning: If a value greater than -1
has been
explicitly set as the width using setWidthChars()
, setting this property will have no effect.
width
- The width you wish to truncate the Label at, in characters.
A value of -1
to remove the override and return
the Label to automatic sizing.public void setSelectable(boolean setting)
public void setUseMarkup(boolean setting)
setting
- If setting is true, then any markup included in the text is
interpreted as such. If its set to false, markup is ignored
and included as-is.public void setUseUnderline(boolean setting)
true
, text of the form
"Op_en a copy"
will result in
Open a copy
The default is false
.
public void setWidthChars(int width)
This sets the width-chars property. Note that if set, this
will take precedence over the max-width-chars property as
set by setMaxWidthChars()
.
This is not precisely reliable as a way to enforce sizing across
different Labels, since the character sizes could be different owing to
differing languages or fonts that may be in use. Still, this is
superior to trying to manually control Widget size by specifying the
horizontal dimension in pixels. If you need a series of Widgets (be
they Labels or otherwise) to have the same size, apply a
SizeGroup
.
width
- The width to base the size of the Label on, in characters. A
value of -1
will turn off the override, and
return the Label to automatically sizing based on the
content within and the max-width property, if
activated.