Delphi FMX TCheckbox/TRadiobutton自动调整大小 [英] Delphi FMX TCheckbox/TRadiobutton Autosize

查看:95
本文介绍了Delphi FMX TCheckbox/TRadiobutton自动调整大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想动态创建具有固定宽度的复选框和单选按钮(在FMX中,不是VCL),以根据其包含的文本更改其高度.我的复选框和单选按钮启用了WordWrap.因此,当启用AutoSize和WordWrap时,我想获得类似于TLabel的功能.

I want dynamically created Checkboxes and Radiobuttons (in FMX, not VCL) with a fixed Width to change their Heigth based on their containing text. My Checkboxes and Radiobuttons have WordWrap enabled. So I want to get something like the TLabel does, when AutoSize and WordWrap are enabled.

我已经设法解决这个问题:

I've already managed to get close to this:

procedure TFMenu.Button1Click(Sender: TObject);
var
  ctr: Integer;
  hostingComponent: TComponent;
begin
  hostingComponent := Form1; { in my case a Frame or Panel or sth. }
  for ctr := 0 to pred(hostingComponent.ComponentCount) do
  begin
    if hostingComponent.Components[ctr] is TRadioButton { or TCheckBox } then
    begin
      with (hostingComponent.Components[ctr] as TRadioButton) do
      begin
        Height := 0; { if text or text Width changes this is useful }
        while (Height) * (Width - 32)
        <=
        (Canvas.TextHeight(Text) * 1.25 ) * Canvas.TextWidth(Text) do
        begin
          Height := Height + 1;
        end;
      end;
    end;
  end;
end;

对于20到150个字符之间的文本,它的工作原理类似于可接受的,但是我知道这是非常错误的,我也不知道,我在这里缺少什么.用 1.25 乘以 Canvas.TextHeight 可以使文本在上述长度下看起来还可以,但是我不知道为什么. Width-32 是因为复选框/单选按钮的图像大小,而不是我要处理的文本.(我猜32是正确的,实际情况可能会有所不同)

This works something like acceptable for texts between 20 and 150 Characters, however I know this is very wrong and I don't know, what I am missing here. Multiplying Canvas.TextHeight by 1.25 made the text look okay for the length mentioned above, but I don't know why. Width - 32 is because of the size of the image of the checkbox/radiobutton, which is not the text I want to handle. (I guessed 32 would be right, it could differ in reality)

所以我的问题是如何根据包含的文本更改这些组件的高度.

也:如何更改代码以按预期工作.它后面的数学运算(除以猜测值1.25的乘法除外)应该是正确的,但是我使用的区域似乎是错误的,因为Checkbox/Radiobutton的区域并不完全包含文本,而是以一些自由空间作为文本结尾不是障碍,而只是左边界,但我不确定这是否重要或如何处理.

Also: How can I change my code to work as expected. The maths behind it (except for the multiplication by the guessed value 1.25) should be correct, but the areas I use seem to be wrong as the area of the Checkbox/Radiobutton does not entirely contain text but end with some free space as the text is not block, but simply left-bounded, but I'm not sure, whether this is important or how to deal with that.

推荐答案

您可以使用 Canvas.MeasureText()方法测量文本,然后设置 TRadioButton .

You can use the Canvas.MeasureText() method to measure the text and then set the size of the TRadioButton.

例如:

procedure TForm22.Button1Click(Sender: TObject);
var
  r: TRectF;
begin
  rb1:= TRadioButton.Create(self);
  rb1.Parent := self;
  rb1.Position.X := 100;
  rb1.Position.Y := 8;
  rb1.Size.Height := 17;
  rb1.Size.Width := 260;

  rb1.StyledSettings := [];
  rb1.TextSettings.Font.Size := 18;
  rb1.TextSettings.WordWrap := True;
  rb1.Canvas.Font.Size := rb1.TextSettings.Font.Size;

  r := RectF(0, 0, rb1.Size.Width-24, 10000);
  rb1.Canvas.MeasureText(r, s, True, [], TTextAlign.Leading, TTextAlign.Leading);

  rb1.Size.Height := r.Bottom;
  rb1.Text := s;
end;

ARect (变量)参数指定文本的边界矩形.该功能使文本适合矩形内部(如果可能).通过指定宽度限制,但高度足够大,该函数将遵循宽度并测量所需的高度(在返回的 TRectF 中进行了修改).

The ARect (var) parameter specifies the bounding rectangle of the text. The function fits the text inside the rectangle (if possible). By specifying the width limit, but a large enough height, the function obeys the width and measures the required height (which is modified in the returned TRectF).

在我的示例中,我从 TRadioButton 宽度中减去了24,以补偿实际的单选按钮.默认样式数据表示21就足够了,但是在计算中我加了3表示安全.对于其他样式,此数字可能会有所不同.请参阅样式数据中 text 成员的 Position.X 属性,以获取 TRadioButton/TCheckBox .

In my example I subtracted 24 from the TRadioButton width to compensate for the actual radiobutton. The default style data indicates 21 would be sufficient, but I added 3 to be on the safe side in the calculation. With other styles this number might be different. See Position.Xproperty of the text member in style data for TRadioButton/TCheckBox.

正如您在我的代码示例中看到的那样,无需使用您使用的1.25因子进行任何调整.我的系统的DPI为100%,如果您的系统为125%,则可以解释您的经验.如果仍然存在问题,则需要检查正在运行程序的系统并根据需要进行补偿.

As you see in my code example, there was no need to adjust anything with the 1.25 factor you used. My system is at 100% DPI, if your is at 125%, it could explain what you experienced. If that is still a problem, you need to check the system that you program is running on and compensate as needed.

最后是一张带有2个单选按钮的窗体的图片,两个单选按钮的宽度分别为width = 260和width = 400.

Finally a picture of a form with 2 radio buttons of width=260 and width=400.

这篇关于Delphi FMX TCheckbox/TRadiobutton自动调整大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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