2017-08-24 98 views
4

我正在使用xamarin格式并面临布局问题。现在,用字体.. 我有两个标签,其中一个是Micro。我需要另一个获得Micro标签/ 2的大小作为它的大小...我正在阅读有关相对布局,但我不知道这是否是最好的方式来做到这一点...有人有任何想法帮我? 这是我的标签:以xamarin格式缩小字体大小

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

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

+1

Micro是最小的可读的字体大小,做这个大小的一半,你将无法正确阅读它,你确定你想这样吗? –

+0

问题是: 在我的ios上的布局微尺寸是大kkkk大很大...然后,xamarin给我的尺寸不太好... –

回答

3

简单地扩展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))); 
    } 
} 

用法示例:

<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

+0

此属性FontSizeFactor附带此脚本,对? 我会尽力告诉你 –

+0

我得到这个“'本地'是一个未申报的前缀。”我需要放在xaml顶端,不是吗? –

+0

是的,你需要声明MyLabel的命名空间 - https://developer.xamarin.com/guides/xamarin-forms/xaml/namespaces/#Declaring_Namespaces_for_Types – Ada

1

所以IMA就如何做到这一点的例子: 我们有2个标签。 首先一个微

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

第二个没有大小

比在可待因的constructur,在最后一行你写:

typelabel.FontSize = statuslabel.FontSize/2; 

的结果: result