FMXTFont (C++)

From RAD Studio Code Examples
Jump to: navigation, search

Description

This example demonstrates how to use different properties of TFont.

This example requires the following components:

  • A TListBox with a number of items (TListBoxItem). Set the items' text to common font names.
  • Four TSpeedButton speed buttons. Set their text to B, I, U, S and their font style to Bold, Italic, Underline, Strikeout, respectively.
  • A spin box (TSpinBox)
  • A label (TLabel)

Create an OnClick event handler for the TSpeedButtons by double-clicking them in Design mode.

The form should look like in the following image.

TFont new1.JPG

Code

Note: To enable direct font changes in FireMonkey, ensure the Font configuration does not depend on the style using the StylesSettings property. If making changes at design time, this is automatic.
// C++

void __fastcall TForm2::FormCreate(TObject *Sender) {
	Form2->Caption = "FMX Font";
	Label1->StyledSettings = TStyledSettings();
    SpinBox1->Min = 6;
	SpinBox1->Max = 72;
	SpinBox1->Value = 12;
	SpinBox1->Increment = 3;
	SpinBox1->OnChange;
}
// ---------------------------------------------------------------------------

void __fastcall TForm2::ListBox1Click(TObject *Sender) {
	Label1->Font->Family = ListBox1->Selected->Text;
}
// ---------------------------------------------------------------------------

void __fastcall TForm2::SpeedButton1Click(TObject *Sender) {
        // fsBold
	TFontStyles AStyle(1);
	if (Label1->Font->Style.Contains(TFontStyle::fsBold))
		Label1->Font->Style = Label1->Font->Style - AStyle;
	else
		Label1->Font->Style = Label1->Font->Style + AStyle;
}
// ---------------------------------------------------------------------------

void __fastcall TForm2::SpeedButton2Click(TObject *Sender) {
        // fsItalic
	TFontStyles AStyle(2);
	if (Label1->Font->Style.Contains(TFontStyle::fsItalic))
		Label1->Font->Style = Label1->Font->Style - AStyle;
	else
		Label1->Font->Style = Label1->Font->Style + AStyle;
}
// ---------------------------------------------------------------------------

void __fastcall TForm2::SpeedButton3Click(TObject *Sender) {
        // fsUnderline
	TFontStyles AStyle(4);
	if (Label1->Font->Style.Contains(TFontStyle::fsUnderline))
		Label1->Font->Style = Label1->Font->Style - AStyle;
	else
		Label1->Font->Style = Label1->Font->Style + AStyle;
}
// ---------------------------------------------------------------------------

void __fastcall TForm2::SpeedButton4Click(TObject *Sender) {
        // fsStrikOut
	TFontStyles AStyle(8);
	if (Label1->Font->Style.Contains(TFontStyle::fsStrikeOut))
		Label1->Font->Style = Label1->Font->Style - AStyle;
	else
		Label1->Font->Style = Label1->Font->Style + AStyle;

}
// ---------------------------------------------------------------------------

void __fastcall TForm2::SpinBox1Change(TObject *Sender) {
	Label1->Font->Size = SpinBox1->Value;
}
// ---------------------------------------------------------------------------

Uses

See Also