Delphi textlayout.textwidth 和 textheight 不起作用,为什么?

问题描述 投票:0回答:1

我正在尝试学习如何使用 TTextLayout 对象向我的图形绘图添加一些特殊字符。 不幸的是,这个对象似乎有一些特殊的行为,这些行为似乎没有记录在 Delphi 帮助文档和 Embarcadero 在线文档中。 我从 Delphi 代码示例中导出了以下代码,添加了一些我需要使用的功能。 Teese的特点是利用TextWidth和TextHeight来获取要绘制的字符的宽度和高度。 为了方便起见,我将正在使用的字体替换为 Arial 字体。 我添加了一些按钮来放大/缩小字符的大小,以及一些其他按钮来选择不同的字符。好吧,当字符发生变化以及大小发生变化时,TTextLayout 对象的宽度和高度似乎没有任何变化。对象的宽度和高度值似乎保持不变。 我已经尝试了我想到的任何内容,将其放在每个语句之前或之后,但什么也没有。 TTextLayout 的宽度和高度似乎保持不变。 我做错了什么?我理解不正确是什么? 像往常一样感谢您的善意帮助。

unit MainGraphicText;
interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs,
  FMX.StdCtrls, FMX.Controls.Presentation, FMX.Objects,FMX.TextLayout, FMX.Edit;

type
  TMainFormGraphicText = class(TForm)
    Image1: TImage;
    Width: TEdit;
    Height: TEdit;
    DimLess: TButton;
    Label1: TLabel;
    DimMore: TButton;
    Label2: TLabel;
    ShowDim: TEdit;
    ShowChar: TEdit;
    CharLess: TButton;
    CharMore: TButton;
    procedure FormCreate(Sender: TObject);
    procedure Image1Paint(Sender: TObject; Canvas: TCanvas; const ARect: TRectF);
    procedure DimLessClick(Sender: TObject);
    procedure DimMoreClick(Sender: TObject);
    procedure CharLessClick(Sender: TObject);
    procedure CharMoreClick(Sender: TObject);
  private
    { Private declarations }
  public
     WhichChar : Integer;
     Dimension : Single;
     MyRect : TRectF;
  end;

var
  MainFormGraphicText: TMainFormGraphicText;

implementation

{$R *.fmx}


procedure TMainFormGraphicText.CharMoreClick(Sender: TObject);
begin
   WhichChar := WhichChar + 1;
   if WhichChar > 80 then
      WhichChar := 80;
   ShowChar.Text := WhichChar.ToString;
   Image1.Repaint;
end;

procedure TMainFormGraphicText.CharLessClick(Sender: TObject);
begin
   WhichChar := WhichChar - 1;
   if WhichChar < 1 then
      WhichChar := 1;
   ShowChar.Text := WhichChar.ToString;
   Image1.Repaint;
end;

procedure TMainFormGraphicText.DimLessClick(Sender: TObject);
begin
   Dimension := Dimension - 1;
   if Dimension < 9 then
      Dimension := 9;
   ShowDim.Text := Dimension.ToString;
   Image1.Repaint;
end;

procedure TMainFormGraphicText.DimMoreClick(Sender: TObject);
begin
   Dimension := Dimension + 1;
   if Dimension > 72 then
      Dimension := 72;
   ShowDim.Text := Dimension.ToString;
   Image1.Repaint;
end;

procedure TMainFormGraphicText.FormCreate(Sender: TObject);
begin
   WhichChar := 40;
   Dimension := 48;
   MyRect := TRect.Create(TPoint.Create(0, 0),Trunc(Image1.Width),Trunc(Image1.Height));
   Canvas.BeginScene;
   Canvas.Stroke.Kind := TBrushKind.Solid;
   Canvas.Stroke.Color := TAlphaColorRec.Black;
   Canvas.Stroke.Thickness := 1.0;
   Canvas.DrawRect(MyRect,1);
   Canvas.EndScene;
end;

procedure TMainFormGraphicText.Image1Paint(Sender: TObject; Canvas: TCanvas; const ARect: TRectF);
Const Acapo = '#13#10';
var
  WPosition: Integer;
  W,H : Single;
  MyLayout: TTextLayout;
begin

  Canvas.BeginScene;
  Canvas.Stroke.Kind := TBrushKind.Solid;
  Canvas.Stroke.Color := TAlphaColorRec.Black;
  Canvas.Stroke.Thickness := 1.0;
  Canvas.DrawRect(MyRect,1);
  Canvas.EndScene;
  MyLayout := TTextLayoutManager.DefaultTextLayout.Create;
  MyLayout.BeginUpdate;
  MyLayout.TopLeft := TPointF.Create(0, 0);
  MyLayout.Font.Family := 'Arial'; // 'Delfos2';
  MyLayout.Font.Size := Dimension;
  MyLayout.Font.Style := [];
  MyLayout.Trimming := TTextTrimming.Character;
  MyLayout.Text := chr(63 + WhichChar); // Copy(Aspetti_Delfos2,WhichChar,1);
  MyLayout.Color := TAlphaColorRec.Brown;
  ShowDim.Text := MyLayout.Font.Size.ToString;
  ShowChar.Text := WhichChar.ToString;
  WPosition := MyLayout.PositionAtPoint(TPointF.Create(0, 0));
  Width.Text := MyLayout.TextWidth.ToString;
  Height.Text := MyLayout.TextHeight.ToString;
  MyLayout.EndUpdate;
  MyLayout.RenderLayout(Canvas);
  MyLayout.Free;
end;
end.
delphi canvas graphics controls firemonkey
1个回答
0
投票

您调用用于检索文本尺寸的代码有点快。由于您之前调用了

MyLayout.BeginUpdate;
,因此您可以阻止对文本布局的任何更改在调用
MyLayout.EndUpdate;
之前生效。

这样做的主要用途是避免频繁更新。但它也会影响要更新的文本布局的一些自身属性,包括

TextWidth
TextHeight

因此,请确保您的

MyLayout.EndUpdate;
在检索文本宽度和文本高度之前,您的代码将按预期工作。

MyLayout.EndUpdate;
Width.Text := MyLayout.TextWidth.ToString;
Height.Text := MyLayout.TextHeight.ToString;
© www.soinside.com 2019 - 2024. All rights reserved.