Delphi FMX TCheckbox/TRadiobutton 自动调整大小
Delphi FMX TCheckbox/TRadiobutton Autosize
我想要动态创建具有固定宽度的复选框和单选按钮(在 FMX 中,而不是在 VCL 中),以根据它们包含的文本更改它们的高度。
我的复选框和单选按钮启用了 WordWrap。
因此,当启用 AutoSize 和 WordWrap 时,我想得到类似于 TLabel 的东西。
我已经接近这个了:
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 个字符之间的文本,此方法类似于可接受的,但是我知道这是非常错误的,我不知道我在这里缺少什么。
将 Canvas.TextHeight
乘以 1.25
使文本看起来符合上述长度,但我不知道为什么。
Width - 32
是因为checkbox/radiobutton的图片大小,不是我要处理的文字。 (我猜32是对的,实际可能不同)
所以我的问题是如何根据包含的文本更改这些组件的高度。
另外:如何更改我的代码以按预期工作。它背后的数学运算(除了乘以猜测值 1.25 之外)应该是正确的,但我使用的区域似乎是错误的,因为 Checkbox/Radiobutton 的区域并不完全包含文本,而是以一些自由 space 因为文本不是块,而只是左边界,但我不确定这是否重要或如何处理。
可以使用Canvas.MeasureText()
的方法测量文字,然后设置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
(var) 参数指定文本的边框。该函数适合矩形内的文本(如果可能)。通过指定宽度限制,但高度足够大,函数服从宽度并测量所需的高度(在返回的 TRectF
中修改)。
在我的示例中,我从 TRadioButton
宽度中减去 24 以补偿实际的单选按钮。默认样式数据表明 21 就足够了,但为了安全起见,我在计算中添加了 3。对于其他样式,此数字可能会有所不同。请参阅 TRadioButton/TCheckBox
样式数据中 text
成员的 Position.X
属性。
如您在我的代码示例中所见,您使用的 1.25 系数无需进行任何调整。我的系统是 100% DPI,如果你的是 125%,它可以解释你的经历。如果这仍然是个问题,您需要检查您编程的系统 运行 并根据需要进行补偿。
最后一张表单图片,其中包含宽度为 260 和宽度为 400 的 2 个单选按钮。
我想要动态创建具有固定宽度的复选框和单选按钮(在 FMX 中,而不是在 VCL 中),以根据它们包含的文本更改它们的高度。 我的复选框和单选按钮启用了 WordWrap。 因此,当启用 AutoSize 和 WordWrap 时,我想得到类似于 TLabel 的东西。
我已经接近这个了:
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 个字符之间的文本,此方法类似于可接受的,但是我知道这是非常错误的,我不知道我在这里缺少什么。
将 Canvas.TextHeight
乘以 1.25
使文本看起来符合上述长度,但我不知道为什么。
Width - 32
是因为checkbox/radiobutton的图片大小,不是我要处理的文字。 (我猜32是对的,实际可能不同)
所以我的问题是如何根据包含的文本更改这些组件的高度。
另外:如何更改我的代码以按预期工作。它背后的数学运算(除了乘以猜测值 1.25 之外)应该是正确的,但我使用的区域似乎是错误的,因为 Checkbox/Radiobutton 的区域并不完全包含文本,而是以一些自由 space 因为文本不是块,而只是左边界,但我不确定这是否重要或如何处理。
可以使用Canvas.MeasureText()
的方法测量文字,然后设置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
(var) 参数指定文本的边框。该函数适合矩形内的文本(如果可能)。通过指定宽度限制,但高度足够大,函数服从宽度并测量所需的高度(在返回的 TRectF
中修改)。
在我的示例中,我从 TRadioButton
宽度中减去 24 以补偿实际的单选按钮。默认样式数据表明 21 就足够了,但为了安全起见,我在计算中添加了 3。对于其他样式,此数字可能会有所不同。请参阅 TRadioButton/TCheckBox
样式数据中 text
成员的 Position.X
属性。
如您在我的代码示例中所见,您使用的 1.25 系数无需进行任何调整。我的系统是 100% DPI,如果你的是 125%,它可以解释你的经历。如果这仍然是个问题,您需要检查您编程的系统 运行 并根据需要进行补偿。
最后一张表单图片,其中包含宽度为 260 和宽度为 400 的 2 个单选按钮。