User:Alexandrab/Using FireMonkey Text Layout Features

From RAD Studio

Go Up to FireMonkey Text Layout

Description

The following example shows how to use new text layout features.

The example contains the procedures describing how to render text on a TImage object and how to use the PositionAtPoint, RegionForRange and ConvertToPath methods.

To replicate this example, create a new FireMonkey HD application and from the Tool Palette, add the following components on your form:

From the Object Inspector create event handlers for the OnCreate event of the TForm, the OnPaint event of the TImage and the OnClick event of the each TButton.

  • The result at desing time:

Code

Add the following code to the OnCreate event handler of the TForm.

procedure TForm1.FormCreate(Sender: TObject);
begin
  RadioButton1.Position.X := Image1.Position.X - 5;
  RadioButton1.Position.Y := Image1.Position.Y - 5;
  RadioButton1.Text := '';
end;


Add the following code to the OnPaint event handler of the TImage.

procedure TForm1.Image1Paint(Sender: TObject; Canvas: TCanvas;
  const ARect: TRectF);
var
  WPosition: Integer;
begin
  MyLayout := TTextLayoutManager.DefaultTextLayout.Create;
  MyLayout.BeginUpdate;
  MyLayout.TopLeft := TPointF.Create(0, 0);
  MyLayout.Font.Size := 78;
  MyLayout.Font.Family := 'Tahoma';
  MyLayout.Font.Style := [TFontStyle.fsBold];
  MyLayout.Text := 'TEXT';
  MyLayout.Color := TAlphaColorRec.Brown;
  WPosition := MyLayout.PositionAtPoint(TPointF.Create(159, 120));
  MyLayout.AddAttribute(TTextRange.Create(WPosition, 3),
    TTextAttribute.Create(TFont.Create, TAlphaColorRec.Black));
  MyLayout.EndUpdate;
  MyLayout.RenderLayout(Canvas);
end;
  • The result at runtime:

Add the following code to the OnClick event handlers of the TButton for Convert Text to Path and Start Animation:

procedure TForm1.Button1Click(Sender: TObject);
var
  PathD: TPathData;
begin
  PathAnimation1 := TPathAnimation.Create(Self);
  PathAnimation1.Parent := RadioButton1;
  PathD := TPathData.Create();
  MyLayout.ConvertToPath(PathD);
  PathAnimation1.Path := PathD;
  PathAnimation1.Loop := True;
  PathAnimation1.Duration := 50;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  PathAnimation1.Start;
end;
  • The result at runtime:

Add the following code to the OnClick event handler of the TButton for RegionforRange:

procedure TForm1.Button3Click(Sender: TObject);
var
  MyRects: TRegion;
begin
  MyRects := MyLayout.RegionForRange(TTextRange.Create(1, 1));
  Image1.Bitmap.Canvas.BeginScene;
  Image1.Bitmap.Canvas.DrawRect(MyRects[0], 0, 0, AllCorners, 100);
  Image1.Bitmap.Canvas.EndScene;
end;
  • The result at runtime:

Uses

See Also