Ro3000
Ro3000

Reputation: 31

How to display a multiline text in a StringGrid Cell (Delphi XE6 - Android)

I have a database bind to a Firemonkey StringGrid and I want to show a long text field in the cells, for an Android App, but I can't find a property nor procedure to do that. Any idea? (Thanks)

Upvotes: 1

Views: 5796

Answers (1)

Ro3000
Ro3000

Reputation: 31

I've found a partial solution to this issue at this link:

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

In the STringGrid's OnDrawColumnCell event (with a little modifications from the original), put this code:

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;

We need to add 'Uses FMX.TextLayout;' to the form and 'System.UIConsts' for the color's constants.

To see a multiline text, of course we need to use a bigger number in the StringGrid's RowHeight property.

Upvotes: 2

Related Questions