I found that this question has been asked before for VCL, but I haven't had any luck getting the answers for that question to work on a Firemonkey TMemo
.
I've noticed that memo.Lines.Count
always seems to match the line count based off how many I add, not as they're formatted (the memo does have wordwrap turned on). Without knowing that number I'm not sure how to start figuring this out.
Any ideas?
Edit: The width of the memo will depend on the orientation of the device, obviously if the width changes the number of lines showing could change. Also, I'd like to not alter the font of the memo.
Procedure ResizeMemo(AMemo: TMemo);
const
Offset = 4; //The diference between ContentBounds and ContentLayout
begin
AMemo.Height := AMemo.ContentBounds.Height + Offset;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
ResizeMemo(Memo1);
end;
The following function should give you what you want. It does not change anything in the memo and takes into account the font and any line breaks and wraps. If setting the memo height to the calculated value, you will need to add a few pixels for borders to eliminate scrollbars.
(add fmx.text to uses statement for XE3, might be different for other versions as they keep changing things with each release)
function get_memo_height(amemo:tmemo):single;
var i:integer;
astring:string;
layout:ttextlayout;
begin
Layout := TTextLayoutManager.DefaultTextLayout.Create;
astring:='';
for i:=0 to amemo.lines.count-1 do astring:=astring+amemo.lines[i]+chr(10);
Layout.BeginUpdate;
Layout.Text :=astring;
Layout.WordWrap := amemo.wordwrap;
Layout.HorizontalAlign := amemo.TextAlign;
Layout.MaxSize := PointF(amemo.width-amemo.VScrollBar.width,maxint);
Layout.VerticalAlign := tTextAlign.taLeading;
Layout.Font := amemo.Font;
Layout.TopLeft := pointf(0,0);
Layout.EndUpdate;
result:=layout.textrect.bottom;
Layout.free;
end;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With