0

我正在尝试在动态创建的TLabel对象中设置字体颜色和字体大小,但它不起作用。

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();
}

结果:

图片

4

1 回答 1

2

您不是TStyledSettings为了启用自己的设置而从中删除项目。请参阅在 C++ 中以编程方式设置 Firemonkey 控制字体

但是你也使用了错误的颜色常数。而不是TColorRec::Red你应该使用TAlphaColor(claRed)

这有效:

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
}
于 2020-05-09T09:33:26.903 回答