【问题标题】:Scalling font size in xamarin forms在 xamarin 表单中缩放字体大小
【发布时间】:2018-02-02 11:47:42
【问题描述】:

我正在使用 xamarin 表单并面临布局问题。现在,使用字体大小.. 我有两个标签,其中一个是 Micro。我需要另一个获得 Micro label / 2 的大小作为它的大小......我正在阅读相对布局,但我不知道这是否是最好的方法......有人有什么想法吗帮我? 这是我的标签:

   <StackLayout Spacing="0">
        <Label x:Name="menu_lbl_promocoes" Text="0000" FontAttributes="Bold" TextColor="Black" HorizontalOptions="Center" Style="{Binding labelsfont}"/>
        <Label x:Name="menu_lbl_disponiveis" Text="Disponíveis" TextColor="Black" HorizontalOptions="Center" FontSize="Small" Style="{Binding labelsfont}"/>
   </StackLayout>

       </StackLayout> //yeah, there is another stacklayout 

       <Label Text="Promoções" FontSize="Micro" TextColor="White" HorizontalOptions="Center" Style="{Binding labelsfont}"/>

我需要第二个标签获得第三个标签的一半(具有微型尺寸)...

【问题讨论】:

  • Micro 是最小的可读字体,把这个大小减半你就无法正常阅读了,你确定要这样吗?
  • 问题是:在我在 ios 上的布局中,微型尺寸很大 kkkk 很大很大......然后,xamarin 给我的尺寸效果不好......

标签: xaml xamarin layout xamarin.forms label


【解决方案1】:

只需扩展 Label 以具有两个可绑定属性 - FontSizeFactor 和 NamedFontSize - 并让它们为您计算字体大小:

public class MyLabel : Label
{
    public static readonly BindableProperty FontSizeFactorProperty =
        BindableProperty.Create(
        "FontSizeFactor", typeof(double), typeof(MyLabel),
        defaultValue: 1.0, propertyChanged: OnFontSizeFactorChanged);

    public double FontSizeFactor
    {
        get { return (double)GetValue(FontSizeFactorProperty); }
        set { SetValue(FontSizeFactorProperty, value); }
    }

    private static void OnFontSizeFactorChanged(BindableObject bindable, object oldValue, object newValue)
    {
        ((MyLabel)bindable).OnFontSizeChangedImpl();
    }

    public static readonly BindableProperty NamedFontSizeProperty =
        BindableProperty.Create(
        "NamedFontSize", typeof(NamedSize), typeof(MyLabel),
        defaultValue: NamedSize.Small, propertyChanged: OnNamedFontSizeChanged);

    public NamedSize NamedFontSize
    {
        get { return (NamedSize)GetValue(NamedFontSizeProperty); }
        set { SetValue(NamedFontSizeProperty, value); }
    }

    private static void OnNamedFontSizeChanged(BindableObject bindable, object oldValue, object newValue)
    {
        ((MyLabel)bindable).OnFontSizeChangedImpl();
    }

    protected virtual void OnFontSizeChangedImpl()
    {
        if (this.FontSizeFactor != 1)
            this.FontSize = (this.FontSizeFactor * Device.GetNamedSize(NamedFontSize, typeof(Label)));
    }
}

示例用法:

    <Label FontSize="Large" Text="Large Size" />
    <local:MyLabel NamedFontSize="Large" FontSizeFactor="0.9" Text="90% Large Size" />

    <Label FontSize="Medium" Text="Medium Size" />
    <local:MyLabel NamedFontSize="Medium" FontSizeFactor="0.75" Text="75% Medium Size" />

    <Label FontSize="Micro" Text="Micro Size" />
    <local:MyLabel NamedFontSize="Micro" FontSizeFactor="0.5" Text="50% Micro Size" />

【讨论】:

  • 这个属性 FontSizeFactor 是这个脚本自带的吧?我会尝试并告诉你
  • 我得到这个“'local'是一个未声明的前缀。”。我需要在 xaml 顶部放一些东西,不是吗?
  • 是的,你需要声明 MyLabel 的命名空间 - developer.xamarin.com/guides/xamarin-forms/xaml/namespaces/…
  • 但它似乎不适用于 android...我不知道为什么
  • 我对此进行了测试 - 效果很好;请查看更新的答案。如果大小差异是一个问题 - 那么您需要使用 OnPlatform 为每个平台指定不同的值
【解决方案2】:

所以我举一个例子来说明如何做到这一点: 我们有 2 个标签。 第一个带微的

 <Label  x:Name="statuslabel" FontSize="Micro" Text="Filter status:" VerticalOptions="Center" WidthRequest="100" />

第二个没有尺寸

比构造函数中的代码,在你写的最后一行:

    typelabel.FontSize = statuslabel.FontSize / 2;

结果:

【讨论】:

    猜你喜欢
    • 2019-04-06
    • 2023-04-01
    • 1970-01-01
    • 2023-03-22
    • 2021-08-22
    • 2013-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多