1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
| private
{ Déclarations privées }
modifierhauteur : boolean;
hauteurligne : single; // hauteur par défaut
public
{ Déclarations publiques }
end;
var
Form1: TForm1;
implementation
{$R *.fmx}
uses FMX.TextLayout, system.Math;
function GetTextHeight(const L: TTextLayout; const Width: single; const
Text: string): Integer;
var
Layout: TTextLayout;
begin
Layout := TTextLayoutManager.DefaultTextLayout.Create;
try
Layout.BeginUpdate;
try
Layout.Font.Assign(L.Font);
Layout.VerticalAlign := TTextAlign.Leading;
Layout.HorizontalAlign := TTextAlign.Leading;
Layout.WordWrap := True;
Layout.Trimming := TTextTrimming.Word;
Layout.MaxSize := TPointF.Create(Width,
TTextLayout.MaxLayoutSize.Y);
Layout.Text := Text;
finally
Layout.EndUpdate;
end;
Result := Round(Layout.Height);
Layout.Text := 'm';
Result := Result + Round(Layout.Height);
finally
Layout.Free;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
modifierhauteur:=true;
Grid1.Repaint;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
memo1.Lines.Clear;
memo1.Lines.Add('toprow '+Grid1.TopRow.ToString);
memo1.Lines.Add('visible '+grid1.VisibleRows.ToString);
memo1.Lines.Add('rowheight '+grid1.RowHeight.ToString);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
modifierhauteur:=true;
hauteurligne:=grid1.RowHeight;
end;
procedure TForm1.Grid1CellClick(const Column: TColumn; const Row: Integer);
begin
showmessage(GetTextHeight(Column.Layout,column.AbsoluteWidth,ClientDataSet1Notes.AsString).ToString);
end;
procedure TForm1.Grid1CreateCustomEditor(Sender: TObject; const Column: TColumn;
var Control: TStyledControl);
begin
showmessage('custom editor');
end;
procedure TForm1.Grid1DrawColumnCell(Sender: TObject; const Canvas: TCanvas;
const Column: TColumn; const Bounds: TRectF; const Row: Integer;
const Value: TValue; const State: TGridDrawStates);
var ATextLayout : TTextLayout;
h,r : single;
begin
if SameText(Column.Header,'notes') then
begin
h:=GetTextHeight(Column.Layout,Bounds.Width,trim(Value.ToString));
if modifierhauteur then begin
if row=Grid1.TopRow then r:=Max(h,Hauteurligne);
if (h>Grid1.RowHeight) AND (row>=Grid1.TopRow) AND (row<=Grid1.TopRow+Grid1.VisibleRows) then
begin
if h>r then grid1.rowheight:=h;
r:= grid1.rowheight;
end;
end;
ATextLayout:=TTextLayoutManager.TextLayoutByCanvas(Canvas.ClassType).Create(Canvas);
ATextLayout.BeginUpdate;
ATextLayout.Text:=Value.ToString;
ATextLayout.Color:=TAlphaColors.Black;
ATextLayout.TopLeft:=Bounds.TopLeft;
ATextLayout.MaxSize := PointF(Bounds.Width,ifthen(modifierhauteur,h,hauteurligne));
ATextLayout.WordWrap:=true;
ATextLayout.VerticalAlign:=TTextAlign.Leading;
ATextLayout.EndUpdate;
ATextLayout.RenderLayout(Canvas);
end
else Column.DefaultDrawCell(Canvas,bounds,row,value,state);
end; |
Partager