Utilisation des fonctions de disposition de texte de FireMonkey

De RAD Studio
Aller à : navigation, rechercher

Remonter à Disposition de texte FireMonkey


L'exemple suivant montre comment utiliser les fonctions de disposition de texte disponibles dans FireMonkey. L'exemple contient du code illustrant comment effectuer le rendu d'un texte sur un objet TImage et comment utiliser les méthodes PositionAtPoint, RegionForRange et ConvertToPath.

Création d'une application multi-périphérique vide

  1. Créez une nouvelle application multi-périphérique vide.
  2. Depuis la palette d'outils, ajoutez les composants suivants à la fiche :
  3. Depuis l'inspecteur d'objets, créez des gestionnaires d'événements pour l'événement OnCreate de TForm, l'événement OnPaint de TImage et l'événement OnClick de chaque TButton.
  • Voici le résultat à la conception :
TextLayout Design Time.png

Ajout du code utilisant la disposition de texte

  1. Ajoutez les déclarations suivantes :

Dans Delphi :

MyLayout: TTextLayout;
PathAnimation1: TPathAnimation;

Dans C++ :

//fichier d'en-tête
TPathAnimation *PathAnimation1;
TTextLayout *MyLayout;
  1. Ajoutez le code suivant au gestionnaire d'événements OnCreate de TForm.

Dans Delphi :

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

Dans C++ :

void __fastcall TForm1::FormCreate(TObject *Sender) {
	RadioButton1->Position->X = Image1->Position->X - 5;
	RadioButton1->Position->Y = Image1->Position->Y - 5;
	RadioButton1->Text = "";
}
2. Ajoutez le code suivant au gestionnaire d'événements OnPaint de TImage.

Dans Delphi :

 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;

Dans C++ :

void __fastcall TForm1::Image1Paint(TObject *Sender, TCanvas *Canvas,
	const TRectF &ARect)

{
	int WPosition;
	Fmx::Graphics::TFont *font;
	MyLayout = TTextLayoutManager::TextLayoutForClass
		(TTextLayoutManager::DefaultTextLayout);
	MyLayout->BeginUpdate();
	MyLayout->TopLeft = TPointF(0, 0);
	MyLayout->Font->Size = 78;
	MyLayout->Font->Family = "Tahoma";
	MyLayout->Font->Style = TFontStyles(1);
	MyLayout->Text = "TEXT";
	MyLayout->Color = TAlphaColorRec::Brown;
	WPosition = MyLayout->PositionAtPoint(TPointF(159, 120));
	MyLayout->AddAttribute(TTextRange(WPosition, 3),
		TTextAttribute(font, TAlphaColorRec::Black));
	MyLayout->EndUpdate();
	MyLayout->RenderLayout(Canvas);
}
  • Voici le résultat à l'exécution :
TextLayout Render.png
3. Ajoutez le code suivant aux gestionnaires d'événements OnClick de TButton afin de convertir le texte en chemin et de démarrer l'animation :

Dans Delphi :

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;

Dans C++ :

void __fastcall TForm1::Button1Click(TObject *Sender) {
	TPathData *PathD;
	PathAnimation1 = new TPathAnimation(this);
	PathAnimation1->Parent = RadioButton1;
	PathD = new TPathData();
	MyLayout->ConvertToPath(PathD);
	PathAnimation1->Path = PathD;
	PathAnimation1->Loop = true;
	PathAnimation1->Duration = 50;
}

void __fastcall TForm1::Button2Click(TObject *Sender) {
	PathAnimation1->Start();
}
  • Voici le résultat à l'exécution :
TextLayout Animation.gif
4. Ajoutez le code suivant au gestionnaire d'événementsOnClick du TButton de RegionforRange :

Dans Delphi :

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;

Dans C++ :

void __fastcall TForm1::Button3Click(TObject *Sender) {
	TRegion MyRects;
	MyRects = MyLayout->RegionForRange(TTextRange(1, 1));
	Image1->Bitmap->Canvas->BeginScene();
	Image1->Bitmap->Canvas->DrawRect(MyRects[0], 0, 0, AllCorners, 100);
	Image1->Bitmap->Canvas->EndScene();
}
  • Voici le résultat à l'exécution :
TextLayout RegionForRange.png

Section uses

Voir aussi