Search code examples
delphifiremonkey

Create Numeric Keyboard Firemonkey Delphi and Dynamic create button with Font are not working


I'm doing a touch system (Windows) that needs numeric keyboard to insert user, manual entry, password, exit system. I prefered to create Button, but the attribute Font are not working.

I created a class:

unit uKeyboard;

interface

uses
  FMX.Edit, FMX.StdCtrls, FMX.Layouts, SysUtils;

type
  TKeyboard = class(TObject)
    btnk  : array [0..9] of TButton;
    btnx  : TButton;
    btnbs : TButton;
    edtk  : TEdit;
    procedure OnClickB (Sender : TObject);
    procedure OnClickX (Sender : TObject );
    procedure OnClickBS (Sender : TObject );
  public
    constructor Create (var GLayout1 : TGridLayout; var EdtText : TEdit);
    destructor Destroy; override;
  end;

implementation

constructor TKeyboard.Create(var GLayout1: TGridLayout; var EdtText: TEdit);
var
  i : Integer;
begin
  edtk := EdtText;
  GLayout1.ItemWidth := Round(GLayout1.Width/3);
  GLayout1.ItemHeight := Round(GLayout1.Height/4);
  for i := 9 downto 0 do
  begin
    btnk[i] := TButton.Create(GLayout1);
    with btnk[i] do
    begin
      Parent := GLayout1;
      TextSettings.Font.Size := 40;
      Text := IntToStr(i);
      OnClick := OnClickB;
    end;
  end;
  ...

Inside Main Unit:

uses
  uKeyboard, ...;

var
  Keyboard : TKeyboard;

// OnClickButton

Keyboard := TKeyboard.Create(GridLayout4,EdtRelease);

The Font is not 40:

image


Solution

  • Per the documentation:

    Setting Text Parameters in FireMonkey: 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.

    So, in order to set a value to the TButton.TextSettings.Font.Size, you need to remove the TStyledSetting.Size flag from the TButton.StyledSettings property, eg:

    btnk[i] := TButton.Create(GLayout1);
    with btnk[i] do
    begin
      Parent := GLayout1;
      StyledSettings := StyledSettings - [TStyledSetting.Size]; // <-- add this
      TextSettings.Font.Size := 40;
      Text := IntToStr(i);
      OnClick := OnClickB;
    end;