在 xamarin.forms 应用中,如何根据默认字体的屏幕大小自定义命名字体大小,这些字体对我不起作用?
mainScale.LabelFontSize = Device.GetNamedSize(NamedSize.Large, typeof(Label)) * 2.5;
我已经硬编码了 2.5,因为它看起来更好。
您能否告诉我如何将这些硬编码更改为跨平台应用程序中的动态因子?
这是一个如何创建大小的想法。
应用xaml.cs
public static double DisplayScreenWidth = 0f;
public static double DisplayScreenHeight = 0f;
public static double Size1 { get; private set; }
public static double Size2 { get; private set; }
public static double Size3 { get; private set; }
if(DisplayScreenHeight > 560)
{
Size1 = Device.GetNamedSize(NamedSize.Large, typeof(Label)) * 1.6;
}
else
{
Size1 = Device.GetNamedSize(NamedSize.Large, typeof(Label)) * 1.2;
}
活动.cs
App.DisplayScreenWidth = (double)Resources.DisplayMetrics.WidthPixels / (double)Resources.DisplayMetrics.Density;
App.DisplayScreenHeight = (double)Resources.DisplayMetrics.HeightPixels / (double)Resources.DisplayMetrics.Density;
App.xaml
xmlns:local="clr-namespace:Mobile" >
<Style x:Key="baseStyle" TargetType="Label">
<Style.Triggers>
<Trigger TargetType="Label"
Property="FontAttributes"
Value="None">
<Setter Property="FontFamily" Value="OpenSans-Regular.ttf#Regular" />
</Trigger>
<Trigger TargetType="Label"
Property="FontAttributes"
Value="Bold">
<Setter Property="FontFamily" Value="OpenSans-Bold.ttf#Regular-Bold" />
</Trigger>
</Style.Triggers>
</Style>
<!-- TAMANHO DE FONTES -->
<Style x:Key="Size1" TargetType="Label" BasedOn="{StaticResource baseStyle}">
<Setter Property="FontSize" Value="{Binding Source={x:Static local:App.Size1}}"/>
</Style>
<Style x:Key="Size2" TargetType="Label" BasedOn="{StaticResource baseStyle}">
<Setter Property="FontSize" Value="{Binding Source={x:Static local:App.Size2}}"/>
</Style>
家.xaml
<label Text="Hello from Xamarin Forms" Style="{StaticResource Size1}"/>
我遇到了艾伦·里奇 GitHub 链接提供的跨平台屏幕分辨率解决方案,而 nuget 包是 Nuget 链接
在共享的 .net 标准库中,在相应的 ContentView 中,以下代码有效:
protected override void OnSizeAllocated(double width, double height)
{
// specific font adjustment code goes here
if (CrossDevice.Hardware.ScreenHeight > 600)
{
...
}
...
base.OnSizeAllocated(width, height); //must be called
}