设置字体设置不适用于创建动态标签

问题描述 投票:0回答:1

我正在尝试在动态创建的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();
}

结果:

image

delphi firemonkey c++builder rad-studio
1个回答
0
投票

您不是要从TStyledSettings中删除项目以启用自己的设置。参见Setting Firemonkey control font programmatically in C++

但是您也使用了错误的颜色常数。代替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
}
© www.soinside.com 2019 - 2024. All rights reserved.