首页 文章

以xamarin形式的字体大小

提问于
浏览
4

我正在使用xamarin表单并面临布局问题 . 现在,使用fontsize ..我有两个标签,其中一个是Micro . 我需要另一个得到Micro标签/ 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}"/>

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

2 回答

  • 5

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

    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)));
        }
    }
    

    Sample Usage:

    <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" />
    

    ios screenshot

    android screenshot

  • 2

    所以ima给出了一个如何做的例子:我们有2个标签 . First one with micro

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

    Second one without size

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

    typelabel.FontSize = statuslabel.FontSize / 2;
    

    结果:
    result

相关问题