Scaling font size in xamarin formats

I am working with xamarin forms and am facing layout issues. Now, with fontsize .. I have two labels, and one of them is Micro. I need another to get the Micro / 2 label size as size ... I read about relative layout, but I don't know if this is the best way to do this ... does anyone have an idea Help me? These are my shortcuts:

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

I need a second label to get half the size of the third (which has a micro size) ...

+4
source share
2 answers

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

ios screenshot

android screenshot

+3

, , : 2 .

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

:

    typelabel.FontSize = statuslabel.FontSize / 2;

: result

+1

Source: https://habr.com/ru/post/1684390/


All Articles