C# Xamarin表单-使用LabelSizeFontToFit效果更改按钮上的文本会使文本越来越小

C# Xamarin表单-使用LabelSizeFontToFit效果更改按钮上的文本会使文本越来越小,c#,xaml,xamarin,xamarin.forms,effects,C#,Xaml,Xamarin,Xamarin.forms,Effects,我在水平堆栈布局中有几个按钮,为了适应不同的语言和纵向,我需要文本大小来适应可用空间。因此,我使用LabelSizeFontToFit效果(来自Xamarin.Toolkit.Effects NuGet包),如下面的XAML代码所示 <StackLayout x:Name="TitleBarLayout" Orientation="Horizontal" HorizontalOptions="Fill" VerticalOptio

我在水平堆栈布局中有几个按钮,为了适应不同的语言和纵向,我需要文本大小来适应可用空间。因此,我使用LabelSizeFontToFit效果(来自Xamarin.Toolkit.Effects NuGet包),如下面的XAML代码所示

<StackLayout x:Name="TitleBarLayout" Orientation="Horizontal" HorizontalOptions="Fill" VerticalOptions="Fill" Spacing="0" Padding="0" Margin="0" BackgroundColor="LightGoldenrodYellow" >
    <Button x:Name="AddSelected" FontSize="Default" HorizontalOptions="End" TextColor="White" VerticalOptions="Center" BackgroundColor="Blue" Margin="0" Padding="0" Clicked="AddSelected_Clicked" >
        <Button.Effects>
            <effects:LabelSizeFontToFit />
        </Button.Effects>
    </Button>
    <Button x:Name="DeleteSelected" FontSize="Default" HorizontalOptions="End" TextColor="White" VerticalOptions="Center" BackgroundColor="Red" Margin="0" Padding="0" Clicked="DeleteSelected_Clicked" >
        <Button.Effects>
            <effects:LabelSizeFontToFit />
        </Button.Effects>
    </Button>
</StackLayout>

这在一开始是正确的。但是,我需要根据是否选择了一个或多个项目,在两个不同的字符串之间切换这些按钮上的文本。不幸的是,每次我更改C代码中的文本时,文本后面的文本都会变小。当我选择和取消选择不同的东西时,它最终变得难以辨认的小。这是没有意义的,因为一旦字体足够小,可以容纳两个字符串,它就不会变小

我尝试过各种布局参数的组合,但都没有效果

有人知道它为什么会这样,以及如何使它正确地工作吗


PS我试过几种其他的收缩字体来适应这种方法,它们更混乱,也更难使用。我不想使用Forms9Patch,因为它会将我的软件包大小增加35%。

对于Xamarin.Toolkit.Effects,不会放大字体大小,只会缩小。要解决此问题,我建议您可以创建自定义按钮,custombtn扩展Xamarin.Forms.Button并添加以下两种方法:

   public class custombtn:Button
{
    /// <summary>
    /// Autosizes the button's font size with regards to it's container size.
    /// </summary>
    private void AutoFontSize()
    {
        //determine the text height for the min font size
        double lowerFontSize = 15;
        double lowerTextHeight = TextHeightForFontSize(lowerFontSize);

        //determine the text height for the max font size
        double upperFontSize = 30;
        double upperTextHeight = TextHeightForFontSize(upperFontSize);

        //start a loop which'll find the optimal font size
        while (upperFontSize - lowerFontSize > 1)
        {
            //determine current average font size and calculate corresponding text height
            double fontSize = (lowerFontSize + upperFontSize) / 2;
            double textHeight = TextHeightForFontSize(upperFontSize);

            //if the calculated height is out of bounds, update max values, else update min values
            if (textHeight > Height)
            {
                upperFontSize = fontSize; upperTextHeight = textHeight;
            }
            else
            {
                lowerFontSize = fontSize; lowerTextHeight = textHeight;
            }
        }

        //finally set the correct font size
        FontSize = lowerFontSize;
    }

    /// <summary>
    /// Determines the text height for the label with a given font size.
    /// </summary>
    private double TextHeightForFontSize(double fontSize)
    {
        FontSize = fontSize;
        return OnMeasure(Width, Double.PositiveInfinity).Request.Height;
    }

    /// <summary>
    /// Callback when the size of the element is set during a layout cycle.
    /// </summary>
    protected override void OnSizeAllocated(double width, double height)
    {
        //call base implementation
        base.OnSizeAllocated(width, height);

        //update font size
        AutoFontSize();
    }

    new public string Text
    {
        get { return (string)GetValue(TextProperty); }
        set { SetValue(TextProperty, value); AutoFontSize(); }
    }

}
公共类custombtn:按钮
{
/// 
///根据按钮的容器大小自动调整按钮的字体大小。
/// 
私有void AutoFontSize()
{
//确定最小字体大小的文本高度
双lowerFontSize=15;
double lowerTextHeight=字体大小的文本高度(lowerFontSize);
//确定最大字体大小的文本高度
双倍大小=30;
double upperTextHeight=字体大小的文本高度(upperFontSize);
//启动一个循环,找到最佳字体大小
while(upperFontSize-lowerFontSize>1)
{
//确定当前平均字体大小并计算相应的文本高度
双字体大小=(lowerFontSize+upperFontSize)/2;
双文本高度=字体大小的文本高度(大写字体大小);
//如果计算的高度超出范围,请更新最大值,否则更新最小值
如果(文本高度>高度)
{
upperFontSize=fontSize;upperTextHeight=textHeight;
}
其他的
{
lowerFontSize=fontSize;lowerTextHeight=textHeight;
}
}
//最后设置正确的字体大小
FontSize=lowerFontSize;
}
/// 
///确定具有给定字体大小的标签的文本高度。
/// 
专用双文本高度FORFONTSIZE(双fontSize)
{
FontSize=FontSize;
测量返回值(宽度,双精度。正精度)。请求。高度;
}
/// 
///在布局周期中设置元素大小时回调。
/// 
IzealLocated上的受保护覆盖空心(双倍宽度,双倍高度)
{
//呼叫基础实现
底座。尺寸定位(宽度、高度);
//更新字体大小
AutoFontSize();
}
新的公共字符串文本
{
获取{return(string)GetValue(TextProperty);}
设置{SetValue(TextProperty,value);AutoFontSize();}
}
}

可以添加MinFontSize和MaxFontSize属性,以便在XAML中指定最小/最大字体大小值。

谢谢。我并不是要求它放大字体,只是不想无缘无故地缩小字体。我会在下班后试用你的代码。我已经试用过这个解决方案,效果不错。非常感谢。