如何在 .Net Maui 中添加基于高度和宽度自动调整标签大小的控件

问题描述 投票:0回答:1

我有一个高度和宽度为 50 的框架,并且我添加了标签“Test”,它应该具有更大的尺寸,如果我添加了“TestTheAutoSize”,它应该根据框架的宽度和高度自动调整字体大小。

xamarin.forms xamarin.android xamarin.ios maui .net-maui.shell
1个回答
0
投票

可以订阅

Frame
的SizeChanged属性的事件,并在后面的代码中写入以下事件代码

private void Frame_SizeChanged(object sender, EventArgs e)
{
    // Get the frame size
    var frameWidth = Frame.Width;
    var frameHeight = Frame.Height;

    // Set the minimum and maximum values for the font size
    var minFontSize = 10;
    var maxFontSize = 30;

    //The fontsize can be adjusted according to the width of the frame, '10' means the ratio and you could change the ratio according to your needs.
    var fontSize = Math.Min(maxFontSize, Math.Max(minFontSize, frameWidth / 10));

    // Set the font size of the label
    Label.FontSize = fontSize;
}
© www.soinside.com 2019 - 2024. All rights reserved.