Wednesday, July 16, 2014

Changing Fire Monkey (FMX) TextSettings at Run Time

One of my beefs with Embarcadero's C++ Builder is that it is so hard to find decent documentation or tutorials on how to do even the simplest things with their visual tools. This is especially true if you're developing in C++ instead of Delphi.

I experienced a pretty steep learning curve with Embarcadero's Visual Class Library (VCL) -- not because it is especially complicated, but because I couldn't find much good documentation for it.

Right now, I am building an FMX application, which is similar in a lot of ways to VCL. However, one thing has changed a lot: the styles are now driven by stylesheets.

For example, in VCL, it was relatively easy to change the font attributes of dynamically created elements at run time. Not so with FMX.

After several hours of fruitless googling, I still haven't figured out how to change my StyleLookup for a dynamically created TEdit. My custom style works fine for controls created at design time, but I'm apparently missing something from the picture.

After some trial and error, I did manage to come up with a solution that pretty closely replicates how text formatting worked in VCL. This obviously isn't ideal, but at this point, I will take what I can get and be happy to move on. The trick is to turn off the FontColor in your object's StyledSettings.

//Make ValueLabel to display output
TLabel * ValueLabel = new TLabel( this );
ValueLabel->Parent = VerifyMappingsScrollBox;
ValueLabel->Position->X = ValueLabelLeft;
ValueLabel->Position->Y = LabelTop;
ValueLabel->Width = ValueLabelWidth;
if ( OutputValue.Length() == 0 )
//Turn it light gray if empty
  ValueLabel->StyledSettings = ValueLabel->StyledSettings >> TStyledSetting::FontColor;
  ValueLabel->TextSettings->FontColor = claLightgray;
  ValueLabel->Text = "Empty";
  ValueLabel->Text = OutputValue;

So far, I'm not too impressed with the FMX framework. It does some cool things, but some elements are so buggy and counter-intuitive that it's easy to see why no one is making new documentation or tutorials for it. Which is a bummer for those of us who are obligated to use it for the foreseeable future.