Verwenden der Textlayout-Funktionen von FireMonkey
Nach oben zu FireMonkey-Textlayout
In folgendem Beispiel wird die Verwendung der in FireMonkey verfügbaren Textlayout-Funktionen demonstriert.
Das Beispiel enthält den Code, der zeigt, wie Text auf einem TImage-Objekt gerendert wird und die Methoden PositionAtPoint, RegionForRange und ConvertToPath verwendet werden.
Inhaltsverzeichnis
Erstellen einer leeren geräteübergreifenden Anwendung
- Erstellen Sie eine neue leere geräteübergreifende Anwendung.
- Ziehen Sie aus der Tool-Palette die folgenden Komponenten auf das Formular:
- 1 TImage
- 1 TRadioButton
- 3 TButtons
- Erstellen Sie im Objektinspektor Ereignisbehandlungsroutinen für das Ereignis OnCreate von TForm, das Ereignis OnPaint von TImage und das Ereignis OnClick für jeden TButton.
-
- Das Formular sollte beim Entwurf folgendermaßen aussehen:
Hinweis: Um das nächste Beispiel zu reproduzieren, müssen Sie eine Hintergrundfarbe in TImage hinzufügen.
Hinzufügen des Codes für das Textlayout
- Fügen Sie die folgenden Deklarationen hinzu:
In Delphi:
MyLayout: TTextLayout;
PathAnimation1: TPathAnimation;
In C++:
//header file
TPathAnimation *PathAnimation1;
TTextLayout *MyLayout;
In Delphi:
procedure TForm3.FormCreate(Sender: TObject);
begin
RadioButton1.Position.X := Image1.Position.X - 5;
RadioButton1.Position.Y := Image1.Position.Y - 5;
RadioButton1.Text := '';
end;
In C++:
void __fastcall TForm3::FromCreate(TObject *Sender)
{
RadioButton1->Position->X = Image1->Position->X - 5;
RadioButton1->Position->Y = Image1->Position->Y - 5;
RadioButton1->Text = "";
}
In Delphi:
procedure TForm3.Image1Paint(Sender: TObject; Canvas: TCanvas;
const ARect: TRectF);
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;
MyLayout.RenderLayout(Canvas);
end;
In C++:
void __fastcall TForm3::Image1Paint(TObject *Sender, TCanvas *Canvas, const TRectF &ARect)
{
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;
}
-
- Zur Laufzeit sieht das Formular nun folgendermaßen aus:
- 3. Fügen Sie den Ereignisbehandlungsroutinen OnClick der TButton-Komponenten den folgenden Code hinzu, um den Text in einen Pfad zu konvertieren und die Animation zu starten:
In Delphi:
procedure TForm3.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 TForm3.Button2Click(Sender: TObject);
begin
PathAnimation1.Start;
end;
In C++:
void __fastcall TForm3::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 TForm3::Button2Click(TObject *Sender)
{
PathAnimation1->Start();
}
- 4. Fügen Sie der Ereignisbehandlungsroutine OnClick der TButton-Komponente für RegionforRange den folgenden Code hinzu:
In Delphi:
procedure TForm3.Button3Click(Sender: TObject);
var
MyRects: TRegion;
begin
MyRects := MyLayout.RegionForRange(TTextRange.Create(1, 1));
if Image1.Bitmap.Canvas.BeginScene then
begin
try
Image1.Bitmap.Canvas.DrawRect(MyRects[0], 0, 0, AllCorners, 100);
finally
Image1.Bitmap.Canvas.EndScene;
end;
end;
end;
In C++:
void __fastcall TForm3::Button3Click(TObject *Sender)
{
TRegion MyRects;
MyRects = MyLayout->RegionForRange(TTextRange(1, 1));
if (Image1->Bitmap->Canvas->BeginScene())
{
try {
Image1->Bitmap->Canvas->DrawRect(MyRects[0], 0, 0, AllCorners, 100);
} __finally {
Image1->Bitmap->Canvas->EndScene();
}
}
}
Verwendet
- TextLayout.TTextLayoutManager
- TextLayout.TTextLayout
- TextLayout.TTextLayout.AddAttribute
- TextLayout.TTextRange
- TextLayout.TTextAttribute
- TextLayout.TTextLayout.RenderLayout
- TextLayout.TTextLayout.PositionAtPoint
- TextLayout.TTextLayout.RegionForRange
- TextLayout.TTextLayout.ConvertToPath