繁体   English   中英

如何设置TMemo高度以填充Delphi firemonkey中的文本内容

[英]How to set the TMemo height to fill text content in Delphi firemonkey

我正在使用XE7。

我在运行时创建了TMemo。

我需要扩大您的高度以显示所有文本行。

我已经尝试过在stackoverflow上找到的这些功能,但是它们无法正常工作。

Memo.height:=get_memo_height(aMemo)Memo.height:=ResizeMemo(aMemo)

我得到的身高小于实际身高。

问候,路易斯

//doesnt work
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.ContentBounds.width,maxint);
      Layout.VerticalAlign := tTextAlign.taLeading;
      Layout.Font := amemo.Font;
      Layout.TopLeft := pointf(0,0);
      Layout.EndUpdate;
      result:=layout.textrect.bottom;
      Layout.free;
    end;



    //doesnt work
    function ResizeMemo(AMemo: TMemo):single;
    const
      Offset = 4; //The diference between ContentBounds and ContentLayout
    begin
      result := AMemo.ContentBounds.Height + Offset;
    end;

尝试以下工作:

将TText组件放在TMemo中,并与mostTop对齐,将Autosize True,HistTest False和Opacity设置为0

然后执行以下操作:

procedure TfnuevoItem.Memo1KeyUp(Sender: TObject; var Key: Word;
  var KeyChar: Char; Shift: TShiftState);
begin
  Text1.Height:=0;
  Text1.Text:=Memo1.Text;
  Text1.RecalcSize;
  Memo1.Height:=Text1.Height+10; // set 10 or more offset
end;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM