【问题标题】:Set font settings dont works on create dynamic Label设置字体设置不适用于创建动态标签
【发布时间】:2020-05-08 19:51:46
【问题描述】:

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

结果:

【问题讨论】:

  • 不确定它是否会有所作为,但请尝试text->TextSettings->FontColor = ...text->TextSettings->Font->Size = ...。但是,text->StyledSettings.Contains(TStyledSetting::Family); text->StyledSettings.Contains(TStyledSetting::Style); 绝对不会像您认为的那样做,请使用:text->StyledSettings = TStyledSettings() << TStyledSetting::Family << TStyledSetting::Style; 或:text->StyledSettings = text->StyledSettings << TStyledSetting::Family << TStyledSetting::Style; 不确定是否还需要添加 TStyledSetting::FontColorTStyledSetting::Size
  • @RemyLebeau 我用你的规格测试过,但结果是一样的 :(
  • 您能否在设计时在表单上创建的TLabel 上设置所需的字体属性?
  • 除了已经说明的内容... 1. TColorRec::Red; 看起来很可疑 我不使用 Delphi 但在 VCL 中:C++ Builder 我会使用 clRed 代替... 2. text->Font->Sizetext->Font->Height 正在相互调整比例。 IIRC 大小是宽度,高度是字体的高度,重新计算的参数将具有负值。此外,IIRC 你不能使用任何尺寸,它们会被舍入到所用字体支持的尺寸...

标签: delphi firemonkey c++builder rad-studio


【解决方案1】:

您不会为了启用您自己的设置而从 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
}

【讨论】:

    猜你喜欢
    • 2021-09-21
    • 2017-08-07
    • 1970-01-01
    • 1970-01-01
    • 2023-03-08
    • 2017-09-11
    • 2020-03-24
    • 2016-08-10
    • 2018-05-22
    相关资源
    最近更新 更多