In a Delphi 10.4.2 32-bit Delphi VCL Application, I have a TLabel
set on top of a TCard
:
object lblColorTransparencyInfo: TLabel
AlignWithMargins = True
Left = 5
Top = 37
Width = 156
Height = 20
Margins.Left = 5
Margins.Top = 5
Margins.Right = 5
Margins.Bottom = 5
Align = alTop
Caption =
'Pick a color in the image to make that color transparent in the ' +
'whole image'
Color = clInfoBk
ParentColor = False
Transparent = False
WordWrap = True
ExplicitTop = 0
end
Label.Color
is set to clInfoBk
, so you can visually check the Label's size.
However, despite the Label.AutoSize
is set to True
, the Label's HEIGHT is much higher than its text height, despite Label.AutoSize = True
:
Is this a bug in TLabel.AutoSize
?
How can I set the Label Height to its correct text-height? (Please note that the Label's width could dynamically change during run-time which would also dynamically change the text-height at run-time).
This is taken from the documentation for the
TCustomLabel.AutoSize
property:It only promises to change the size when the text or font is changed -- not when the label is resized due to its parent being resized. So one could argue that there is no bug here:
But in any case, one very quick and dirty solution is to tell the label to autosize when it is resized. Using an interposer class,
we can make it work (almost):
Of course, you could make your own
TLabelEx
control with this addition so you can use it as easily as the standard label.