Setting Text Parameters in FireMonkey
Contents
Go Up to FireMonkey Application Design
In the TTextSettings class and the ITextSettings interface, FireMonkey provides a universal tool for managing appearance parameters of text objects.
Visual Text Parameters
The TTextSettings class declares the TFont.Family, TFont.Size, TFont.Style, FontColor, HorzAlign, VertAlign, Trimming, WordWrap, and FontColorForState properties defining visual parameters of a text representation and methods managing these properties in components rendering text objects. The TTextSettings class defines features providing the possibility to specify how output texts are drawn in components. For example, you can write:
Delphi:
Label1.TextSettings.FontColor := MyColor;
C++:
Label1->TextSettings->FontColor = MyColor;
This code sets the published property TLabel.FontColor of the TLabel type object, which actually inherits the TTextSettings.FontColor property. However, to use TTextSettings properties, first one needs to know the type of the component (TLabel in this example).
Using the ITextSettings Interface
The ITextSettings interface declares methods and properties to manage visual parameters of a text representation in the TTextSettings type text objects independently of particular types containing the components.
Classes using the TTextSettings type text objects TMemo, TCustomEdit, TTextControl, and their descendants have the public property TextSettings of the TTextSettings type and they implement the ITextSettings interface. Using the methods and the properties declared in ITextSettings, you can manage text representation properties declared in TTextSettings in the component type independent style.
For example, you might not know the particular type of a text object in a component. For example, a text object can be of the TText or TTextControl type. These classes have the Color and FontColor color properties respectively. Generally, to set the color to one of these properties, one needs to firstly check the type of an object instance and then cast the color type as follows:
if Obj is TText then TText(Obj).Color := MyColor else if Obj is TTextControl then TTextControl(Obj).FontColor := MyColor;
The usage of the ITextSettings interface makes this task much more simple and universal:
Delphi:
procedure TForm12.Button1Click(Sender: TObject);
var
Settings: ITextSettings;
Instance: TComponent;
I: Integer;
begin
Instance := Button1;
for I := 0 to ChildrenCount - 1 do
begin
Instance := Children[I];
if IInterface(Instance).QueryInterface(ITextSettings, Settings) = S_OK then
begin
// using ITextSettings methods and properties:
// TextSettings: TTextSettings,
// DefaultTextSettings,
// StyledSettings
// to change properties of text objects
Settings.TextSettings.BeginUpdate;
try
Settings.TextSettings.Font.Size := 18;
if TStyledSetting.Size in Settings.StyledSettings then
Settings.StyledSettings := Settings.StyledSettings - [TStyledSetting.Size]
// show Font.Size := 18
else
Settings.StyledSettings := Settings.StyledSettings + [TStyledSetting.Size];
// restore showing Font.Size loaded from a style
finally
Settings.TextSettings.EndUpdate;
end;
end;
end;
end;
C++:
void __fastcall TForm1::Button1Click(TObject *Sender) {
_di_ITextSettings Settings;
TComponent* Instance = new TComponent(Form1);
int I;
Instance = Button1;
for (I = 0; I < ChildrenCount ; I++) {
Instance = Children->Items[I];
if (Instance->GetInterface(Settings)) {
Settings->TextSettings->BeginUpdate();
try {
Settings->TextSettings->Font->Size = 18;
if (Settings->StyledSettings.Contains(TStyledSetting::Size)) {
Settings->StyledSettings =
Settings->StyledSettings >> TStyledSetting::Size;
}
else {
Settings->StyledSettings =
Settings->StyledSettings << TStyledSetting::Size;
}
}
catch (Exception* e) {
Settings->TextSettings->EndUpdate();
}
Settings->TextSettings->EndUpdate();
}
}
}
You can just retrieve the ITextSettings interface type object in the Settings
variable. In case of success, the return value of Settings
shouldn't be nil
. In this case, the particular type of Instance
is not important. What is important is that the obtained IInterface(Instance)
interface object contains the following properties: TextSettings,
DefaultTextSettings,
StyledSettings. Using these properties, you can change text properties of any types of the TTextSettings type text objects.
Using the StyledSettings Property
When changing text representation properties of the TTextSettings type objects, remember that when you are changing the value of a property (of the TextSettings.Font.Size
property in the previous example), then the actual changing of the object's view happens only if the ITextSettings.StyledSettings property does not contain the TStyledSetting.Size constant. The "Relation between TStyledSetting constants and TTextSettings properties" table shows which TStyledSetting constants control handling of the TTextSettings text representation properties.