8

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?

Sae1962
  • 1,122
  • 15
  • 31
Arnold
  • 4,578
  • 6
  • 52
  • 91
  • FireMonkey is not like that at all. You need to learn about styles. Start with the program docs. – David Heffernan Feb 02 '14 at 20:10
  • That does not explain why I can change the font color by manually setting a property and not by doing the same in my program. – Arnold Feb 02 '14 at 20:16
  • 2
    You need to exclude the [`ssFontColor`](http://docwiki.embarcadero.com/Libraries/XE5/en/FMX.Graphics.ITextSettings.StyledSettings) style from the label's [`style set`](http://docwiki.embarcadero.com/Libraries/XE5/en/FMX.Controls.TTextControl.StyledSettings), otherwise the font color from the assigned style will be applied to the label. (if it uses the same logic as in Delphi XE3) – TLama Feb 02 '14 at 20:23
  • @TLama - ssFontColor was already excluded but ssStyle did the trick. If you make that an answer I'll accept it as thanks for showing me this suggestion, that led me to the correct solution. – Arnold Feb 02 '14 at 20:24
  • It does indeed explain that. Did you find the documentation? – David Heffernan Feb 02 '14 at 20:25
  • @David - of course: "Embarcadero Technologies does not currently have any additional information. Please help us document this topic by using the Discussion page" – Arnold Feb 02 '14 at 20:29
  • 1
    You must be looking at different pages. TLama's links, and the pages I found were clear. Docs for fmx are a bit sketchy though I agree – David Heffernan Feb 02 '14 at 20:36
  • 1
    @Arnold, I would rather let this answer someone experienced or at least someone who has Delphi XE5. I'll be silently waiting at least 1 or 2 years before I start to think to write something serious on FMX :-) – TLama Feb 02 '14 at 20:44
  • 4
    I was jumping to conclusions a bit too fast. To programmatically change the `FontColor` the following works: `FText.StyledSettings := FText.StyledSettings - [TStyledSetting.ssFontColor]; FText.FontColor := TAlphaColors.Cyan;` (where FText is a `TLabel`). The docs with the dreaded Embarcadero lack of effort in documentation was about `FontColor`. TLama's answer and link were the correct one. Thanks to both TLama and David for their help and persistence ;-) – Arnold Feb 02 '14 at 20:49

4 Answers4

9

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];
Sae1962
  • 1,122
  • 15
  • 31
Nicolas Dusart
  • 1,867
  • 18
  • 26
2

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

pskiebe
  • 93
  • 6
1

Sub a TLabel for TText Control. problem solved !

ThisGuy
  • 1,405
  • 1
  • 24
  • 51
  • What exactly do you mean? – Arnold Feb 03 '14 at 21:03
  • I ran into the same issue where the Color was not sticking for TLabels. Instead of a using a TLabel, use a TText Control. You can code something like - 'MyText.Color := TAlphaColors.Dodgerblue;' – ThisGuy Feb 04 '14 at 15:53
  • Allows you to avoid style sheets and etc. – ThisGuy Feb 04 '14 at 15:54
  • Thanks for mentioning this solution, didn't know it. Anyhow, my problem with TLabel was solved by TLama and mentioned in my comment. – Arnold Feb 04 '14 at 20:03
-1

We can change TLabel color programmatically as:

Label1.Font.Color := clBlue;
sagar
  • 704
  • 8
  • 9