2017-08-24 161 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