35 Votes

Delphi: Center the Text on a Label vertical and horizontal

Question by Guest | Last update on 2021-04-19 | Created on 2012-08-27

I want to center the caption of a TLabel  vertically and horizontally, the text should therefore appear exactly in the middle, when I change the size of the label.

However, the text is always sticking at the top. But instead, the text should always be in the middle, just as it is with the panel (TPanel). How can I achieve this? Unfortunately, I can not find a property for VerticalAlign with the label.

12Best Answer24 Votes

You just have to set the properties of "Alignment" and "Layout" in the Object Inspector or the code to centered. In the code, that is looking like this:

Label1.Alignment := taCenter;
Label1.Layout    := tlCenter;

Alignment ensures the horizontal alignment, Layout provides the vertical alignment.

Under certain circumstances, you have also to set "AutoSize" to "False", in order to prevent the reset of the size of the label.
Last update on 2021-04-19 | Created on 2012-08-28

ReplyPositive Negative
-1721 Votes

It dosn't work.

Working solution:

(label1.Width-label1.Width) div 2;

(label1.Height-label1.Height) div 2;

and "AutoSize" to "False".
2013-11-26 at 12:02

Positive Negative
99 Votes

But the .Left and .Top properties of the label have no effect to the alignment of the text of the label.

And .Width - .Witdh is always zero leading to exceptions as it is also for .Height - .Height.
2013-11-26 at 15:05

Positive Negative

Related Topics

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.


Ask your own question or write your own article on askingbox.com. That’s how it’s done.