如何在StringGrid单元格中显示多行文本(Delphi XE6 - Android)

时间:2014-11-09 02:25:13

标签: delphi cell firemonkey multiline stringgrid

我有一个数据库绑定到Firemonkey StringGrid,我想在单元格中显示一个长文本字段,对于Android应用程序,但我找不到属性或程序来执行此操作。任何的想法? (感谢)

1 个答案:

答案 0 :(得分:2)

我在此链接中找到了对此问题的部分解决方案:

http://fire-monkey.ru/topic/287-izmenenie-svoistva-shrifta-odnoi-iacheiki-v-firemonkey-tstringgrid-delphi-xe6/#entry1041

在STringGrid的OnDrawColumnCell事件中(稍加原始修改),请输入以下代码:

procedure TForm1.StringGrid1DrawColumnCell(Sender: TObject; const Canvas: TCanvas; const Column: TColumn; const Bounds: TRectF; const Row: Integer; const Value: TValue; const State: TGridDrawStates);
const
   HorzTextMargin = 2;
   VertTextMargin = 1;
var
   TextLayout : TTextLayout;
   TextRect: TRectF;
begin
   // Here we determine which cell will redraw 
   if (Column.Index=0) then
   begin
      TextRect := Bounds;
      TextRect.Inflate(-HorzTextMargin, -VertTextMargin);
      Canvas.FillRect(Bounds, 0, 0, AllCorners, 1);
      TextLayout := TTextLayoutManager.DefaultTextLayout.Create;
      try
         TextLayout.BeginUpdate;
         try
            TextLayout.WordWrap := True; // True for Multiline text 
            TextLayout.Opacity := Column.AbsoluteOpacity;
            TextLayout.HorizontalAlign := StringGrid1.TextSettings.HorzAlign;
            TextLayout.VerticalAlign := StringGrid1.TextSettings.VertAlign;
            TextLayout.Trimming := TTextTrimming.Character;
            TextLayout.TopLeft := TextRect.TopLeft;
            TextLayout.Text := Value.ToString;
            TextLayout.MaxSize := PointF(TextRect.Width, TextRect.Height);

            { Custom settings rendering }
            TextLayout.Font.Family := 'Times New Roman';
            TextLayout.Font.Style := [ TFontStyle.fsBold ];
            TextLayout.Font.Size := 14;
            TextLayout.Color := claBlueViolet;
         finally
            TextLayout.EndUpdate;
         end;
         TextLayout.RenderLayout(Canvas);
      finally
         TextLayout.Free;
      end;
   end;
end;

我们需要添加'使用FMX.TextLayout;'到形式和'System.UIConsts'为颜色的常量。

要查看多行文本,我们当然需要在StringGrid的RowHeight属性中使用更大的数字。