Search code examples
delphifiremonkeyc++builderrad-studio

Set font settings dont works on create dynamic Label


I am trying to set the font color and font size in a dynamically created TLabel object, but it does not work.

void __fastcall TForm1::Button1Click(TObject *Sender)
{
    TLabel *text;
    text = new TLabel(Form1);
    text->Parent = Form1;
    text->Align = TAlignLayout::Center;
    text->Margins->Top = 60;
    text->Font->Size = 13;   // don't works
    text->FontColor = TColorRec::Red;  // don't works
    text->Height = 17;
    text->Width = 120;
    text->TextSettings->HorzAlign = TTextAlign::Center;
    text->TextSettings->VertAlign = TTextAlign::Leading;
    text->StyledSettings.Contains(TStyledSetting::Family);
    text->StyledSettings.Contains(TStyledSetting::Style);
    text->Text = "My Text";
    text->VertTextAlign = TTextAlign::Leading;
    text->Trimming = TTextTrimming::None;
    text->TabStop = false;
    text->SetFocus();
}

Result:

image


Solution

  • You are not removing items from TStyledSettings in order to enable your own settings. See Setting Firemonkey control font programmatically in C++

    But then you are also using wrong color constants. Instead of TColorRec::Red you should use TAlphaColor(claRed)

    This works:

    void __fastcall TForm1::Button1Click(TObject *Sender)
    {
        TLabel *text;
        text = new TLabel(Form1);
        text->Parent = Form1;
        text->Position->X = 8;
        text->Position->Y = 50;
        text->Text = "My Text";
    
        // clear all styled settings to enable your own settings
    //  text->StyledSettings = TStyledSettings(NULL);
    
        // alternatively clear only styled font color setting
        text->StyledSettings = text->StyledSettings >> TStyledSetting::FontColor;
    
        // and styled size setting
        text->StyledSettings = text->StyledSettings >> TStyledSetting::Size;
    
        // Firemonkey uses TAlphaColor colors
        text->FontColor = TAlphaColor(claRed);
        // alternatively:
        // text->FontColor = TAlphaColor(TAlphaColorRec::Red);
        // text->FontColor = TAlphaColor(0xFFFF0000); // ARGB
    
        text->Height = 20;
        text->Font->Size = 15;   // works now
    }