How to programmatically change the FontColor of a Tlabel

22,083

Solution 1

To enable modifying font color of a TLabel object, you need to change its StyledSettings property.

It's an array defining the different settings that are defined by the current style and cannot be changed by other means.

To be able to change the color of the font, you have to remove the TStyledSetting.FontColor entry from this array.

You can do it programmatically with

Label1.StyledSettings := Label1.StyledSettings - [TStyledSetting.FontColor];

or from the Object Inspector in the designer, select your label, go in StyledSettings and untick FontColor.

Other settings that can be fixed by the current style are

  • TStyledSetting.Family
  • TStyledSetting.Size
  • TStyledSetting.Style
  • TStyledSetting.Other

So, for being able to change the font color and the size, you would write:

Label1.StyledSettings := Label1.StyledSettings - [TStyledSetting.FontColor, TStyledSetting.Size];

Solution 2

Sub a TLabel for TText Control. problem solved !

Solution 3

I just tried the answer from @NicolasDusart and found out, that a TLabel doesn't seem to have StyledSettings. I guess this changed in newer versions of Delphi, I'm currently using Delphi Tokyo. However with this I was able to change the font color:

Label1.StyleElements := Label1.StyleElements - [seFont];

The Delphi documentation lists TStyleElements as as set:

type TStyleElements = set of (seFont, seClient, seBorder);

Vcl.Controls.TStyleElements

Share:
22,083
Arnold
Author by

Arnold

Updated on July 12, 2022

Comments

  • Arnold
    Arnold almost 2 years

    When I put a TLabel on a form, I can change the color of its text by changing the FontColor property. However, when I do this in my program by

    Label1.FontColor := TAlphaColors.Aquamarine;
    

    this doesn't work. Any idea what's wrong?