如何调整列表框项目的高​​度以适合Firemonkey(机器人)的文本? [英] How to adjust ListBox items height to fit the text in Firemonkey (android)?

查看:979
本文介绍了如何调整列表框项目的高​​度以适合Firemonkey(机器人)的文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用这个code将项目添加到列表框,但我无法弄清楚如何动态调整项的高度以适合文本:

I'm using this code to add an item to a listbox, but I can't figure out how to dynamically resize the height of the item to fit the text:

procedure TmForm.AddItemBtnClick(Sender: TObject);
var
  Item: TListBoxItem;
begin
  Item := TListBoxItem.Create(nil);
  Item.Parent := SomeListBox;
  Item.StyleLookup := 'listboxitemstyle';
  Item.Text :=
              'Pe cararea lunga scurta se ducea un om venind, si-n tacerea lui ' +
              'profunda se auzea borborosind. Cantr-o noapte intunecoasa soarel' +
              'e lucea pe cer, iara eu cu barca in casa ma plimbam ca un boier.';
  Item.WordWrap := true;
  Item.StyledSettings := [TStyledSetting.ssFamily] + [TStyledSetting.ssStyle] + [TStyledSetting.ssFontColor];
  Item.Font.Size := 14;
end;

我试图使用从code <一个href=\"http://stackoverflow.com/questions/18430290/how-to-adjust-button-size-to-fit-the-text-in-delphi-firemonkey\">this例如(修改TListBoxItem),但没有奏效。

I tried using the code from this example (modified for TListBoxItem), but it didn't work.

编辑:的ListBoxItem中的高度可以只需添加 Item.Height设置:= 100; 在$ C的结束$ C以上,但我需要知道的文字来决定什么大小ListBoxItem中必须的高度,所以要澄清我的问题,我如何才能在列表框中项文本的高度?

The height of the ListBoxItem can be set by just adding Item.Height := 100; at the end of the code above, but I need to know the height of text to decide what size the ListBoxItem needs to be, so to clarify my question, how do I get the height of the text in the list box item?

推荐答案

正如迈克·萨顿所提到的,这是可以做到 OnApplyStyleLookup 事件。
我把它用做 TTextLayout

As mentioned by Mike Sutton, it can be done OnApplyStyleLookup event. I do it using a TTextLayout :

uses 
... ,FMX.TextLayout;

procedure TfrmForm1.ListBoxItem1ApplyStyleLookup(Sender: TObject);
var
  myLayout: TTextLayout;
  aPoint: TPointF;
begin

  myLayout := TTextLayoutManager.DefaultTextLayout.Create;
  myLayout.BeginUpdate;

  // Setting the layout MaxSize
  aPoint.X := ListBoxItem1.Width;
  aPoint.Y := TfrmForm1.Height;
  myLayout.MaxSize := aPoint;

  myLayout.Text := ListBoxItem1.Text;
  myLayout.WordWrap := True ;
  myLayout.Font := ListBoxItem1.Font;
  myLayout.HorizontalAlign := ListBoxItem1.TextSettings.HorzAlign;
  myLayout.VerticalAlign := ListBoxItem1.TextSettings.VertAlign;
  myLayout.Padding := ListBoxItem1.Padding;
  // set other properties as needed        

  myLayout.EndUpdate;

  ListBoxItem1.Height := Trunc(myLayout.TextHeight) + 3 ; // +3px to be sure to see entire text

end;

注意 MAXSIZE 的移动限制。例如, aPoint.Y 将限制最后 textHeight不同。你应该把它很大,因为,无论 textHeight不同应该是,如果 myLayout.TextHeight 大于 myLayout.MaxSize.Y 然后 myLayout.TextHeight 将被设置为 myLayout.MaxSize.Y

Note that MaxSize is limitating. For example, aPoint.Y will limit the final TextHeight. You should set it large because, whatever TextHeight should be, if myLayout.TextHeight is larger than myLayout.MaxSize.Y then myLayout.TextHeight will be set to myLayout.MaxSize.Y.

这里的的列表 TTextLayout 属性。

Here's a list of TTextLayout properties.

这篇关于如何调整列表框项目的高​​度以适合Firemonkey(机器人)的文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆