在OnDrawColumnCell
和/或OnDrawColumnHeader
事件中,您可以使用 aTTextLayout
来达到目的。如以下示例所示,使用三种不同的对齐方式绘制单元格。绘制标题时也可以应用相同的方法:
uses
...
fmx.textlayout;
procedure TForm11.Grid1DrawColumnCell(Sender: TObject; const Canvas: TCanvas;
const Column: TColumn; const Bounds: TRectF; const Row: Integer;
const Value: TValue; const State: TGridDrawStates);
var
tl: TTextLayout;
rf: TRectF; // added
begin
tl := TTextLayoutManager.DefaultTextLayout.Create;
try
tl.BeginUpdate;
try
// added from here
rf := Bounds;
InflateRect(rf, -2, -2);
if (TGridDrawState.Selected in State) or
(TGridDrawState.Focused in State) or
(TGridDrawState.RowSelected in State)
then
Canvas.Fill.Color := TAlphaColors.LightBlue
else
Canvas.Fill.Color := TAlphaColors.White;
Canvas.FillRect(rf, 0, 0, [], 1);
// added until here
tl.TopLeft := Bounds.TopLeft;
tl.MaxSize := PointF(Column.Width, Column.Height);
tl.Font.Size := 15;
tl.Text := 'Some text'; // Value
case Column.Index of
0: tl.HorizontalAlign := TTextAlign.Leading;
1: tl.HorizontalAlign := TTextAlign.Center;
2: tl.HorizontalAlign := TTextAlign.Trailing;
end;
finally
tl.EndUpdate;
end;
tl.RenderLayout(Canvas);
finally
tl.Free;
end;
end;

TTextLayout
还有许多其他有用的选项和属性,所以我建议看一下文档。