TLabel is a component that creates a text-item with one or more lines on another component. A TLabel is a descendant of TGraphicControl and is available under the Standard tab of the Component Palette.
Usage
editA TLabel is one of the most basic components that can be used on forms. Most labels are the marking of other components, such as Edit fields, Memos, StringGrids and so on. TLabel provides a variety of events but they are not required in most cases.
You can add a label to your form, by clicking the TLabel (palette icon Abc) on the Standard component palette and place it with a click on your form.
Caption
editTo change the default caption of a newly inserted TLabel on a form, you can proceed as follows:
- On your form with one click, select the TLabel.
- Go on properties in the Object Inspector tab.
- Select the property Caption and change it in the adjacent edit field.
- In the same way, you can select the property Name and give the label a better name.
Changing the caption at run time
editOf course, you can change the caption (the text displayed) during run time.
The following example demonstrates this:
- Create a new GUI application with the form Form1. Add this form still a TButton Button1 and a TLabel Label1 by selecting the appropriate components on the Standard Component Tab and clicking on Form1 (the label should be above the button).
- Create now a event handler for Button1, by simply double clicking on Button1.
- Insert following lines of code in the
OnClickevent handler of Button1:
procedure TForm1.Button1Click(Sender: TObject);
const Cnt: Integer = 0; //Counter to determine how many times the button has been clicked
begin
inc(Cnt); //Increment the counter by 1
Label1.Caption:='Button was clicked ' + //Write the text on the caption of Label1
IntToStr(Cnt) + ' times';
end;
- Start your program and test the change of the label caption by clicking the button.
Text placement
editAlignment
editThe horizontal position of the label text within the enclosing bounds is determined by the Alignment property:
taLeftJustify- the text begins at the left side of the controltaRightJustify- the text is moved to the right so that it ends at the right side of the controltaCenter- the text is centered horizontally within the width of the writing block.
In case of multi-lined or word-wrapped text (see below) the individual lines are positioned accordingly.
Layout
editThe Layout property refers to the vertical alignment of the caption text. It has the options
tlTop- the text begins at the top edge of the controltlCenter- the text is centered vertically within the height of the control.tlBottom- the text is moved downward so that it ends at the bottom edge of the control.
Delphi incompatibility for right-aligned labels
edit- In Delphi, autosized labels with
Alignment=taRightJustifybutAnchors=[akLeft,...]grow to the left. In LCL they grow to to right, starting with Lazarus 2.3.0. - Reason: It wasn't possible to implement the behavior also for hidden labels without significant extensions in the LCL. The LCL has a different and more generic feature of control-based anchoring that delivers the same effect (see Remedy down), so it is not needed and wanted to double this feature and make the LCL code more complex and prone to bugs.
- Remedy: Use the LCL anchoring to a secondary control. Anchor the right side of the label to another control. Then the autosized label will grow to the left but won't move to the right when the parent is resized like it is done with a simple akRight anchor without a reference control.
Multi-lined labels
editLabels can be multi-lined in two ways
Hard-coded line-breaks
editThis can be achieved by inserting a LineEnding string as line-break indicator into the Caption text. As this is the default behaviour, no special property needs to be set for this to occur.
Example:
Label1.Caption := 'This is line 1,' + LineEnding + 'and here is the line 2.';
WordWrap
editAutomatic wordwrap occurs when the Caption is longer than a given pixel length and when the Wordwrap property of the label has been switched to true. The length limit beyond which the text is wrapped can be defined in several ways:
- by the width of the label's
Parentwhen the label has theAlignproperty set totaToportaBottom - by
Constraints.MaxWidthbeing non-zero - by anchoring to other controls
- In Laz 4.99, a further restriction by the
WordWrapLengthhas been introduced which is important especially when a label is rotated (see below). It becomes active when it has a positive, non-zero value.
Without such restrictions, the label width increases with the caption text, even when WordBreak=true.
Label rotation
editA label can be rotated in any direction when the Orientation property of the label's Font is set to a non-zero value. The value given is given in units of 1/10 degree with positive numbers in counter-clockwise direction and with zero indicating the horizontal 3 o'clock direction.
When the WordWrap property is set to true for rotated text the position at which word-breaks are introduced is defined by the property WordWrapLength (in pixels).
AutoSize
editWhen the AutoSize property of the label is set to true the Width and Height values are adjusted to fit the size of the text. In case of a rotated label the smallest rectangle is calculated which tightly encloses the text.
Advanced demo: A LinkLabel
editSometimes a click on a label should open a specific URL in the browser. Here are the steps required to open the Lazarus homepage by a label click:
- Drop the label on the form, it will be named "LinkLabel" subsequently (set the
Nameproperty to "LinkLabel"). - Set its
Captionto "Lazarus". - Specify the URL of the Lazarus site in the
Hintproperty,'https://www.lazarus-ide.org/'. To see the hint at mouse-over, make sure that the label'sShowHintistrue(or the form'sShowHint=trueand the label'sParentHint=true). - Now provide an
OnClickevent handler to open this URL in the brower:
uses
LCLIntf; // for OpenURL
procedure TForm1.LinkLabelClick(Sender: TObject);
begin
if Sender is TLabel then
OpenURL(TLabel(Sender).Hint);
end;
- Usually URL links are painted in blue color: In the Object Inspector, set the label's
Font.ColortoclBlue. - And when the mouse hovers over the label, the caption should become underlined. This can be achieved by providing handlers for the
OnMouseEnterandOnMouseLeaveevents:
procedure TForm1.LinkLabelMouseEnter(Sender: TObject);
begin
with (Sender as TControl).Font do
Style := Style + [fsUnderline];
end;
procedure TForm1.LinkLabelMouseLeave(Sender: TObject);
begin
with (Sender as TControl).Font do
Style := Style - [fsUnderline];
end;
- Finally, the mouse cursor should change to the "hand-point" shape when the mouse is over the label: Set the label's
CursortocrHandPoint. - The following code block summarizes all changes that can be made in the object inspector:
procedure TForm1.FormCreate(Sender: TObject);
begin
LinkLabel.Caption := 'Lazarus';
LinkLabel.Hint := 'https://www.lazarus-ide.org/';
LinkLabel.ShowHint := true;
LinkLabel.Font.Color := clBlue;
LinkLabel.Cursor := crHandPoint;
LinkLabel.OnClick := @LinkLabelClick;
LinkLabel.OnEnter := @LinkLabelMouseEnter;
LinkLabel.OnEnter := @LinkLabelMouseLeave;
end;