1
votes

I want to make a label appear the same size proportionally regardless of the resolution of the target device

I have the following code

<StackLayout 
     Orientation="Vertical" Margin="0,0,0,0" Padding="0,0,0,0" 
     HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
         <Label Text="{Binding AccountName}" 
                Style="{StaticResource labelStylePrimaryBold}"
                HorizontalOptions="StartAndExpand" />
</StackLayout>

I have the above code and this in my App.xaml

  <Style x:Key="labelStylePrimaryBold" TargetType="Label">
    <Setter Property="TextColor" Value="#414042" />
    <Setter Property="FontAttributes" Value="Bold" />
    <Setter Property="FontSize" Value="15" />
  </Style>

I need to make this available across my app, I've seen the following code on https://developer.xamarin.com/guides/xamarin-forms/user-interface/text/fonts/

label.FontSize = Device.OnPlatform (
     24,
     Device.GetNamedSize (NamedSize.Medium, label),
     Device.GetNamedSize (NamedSize.Large, label)
);

But my label does not have an ID, how would I go about linking this up?

I'm very new to Xamarin, but this seems like a fairly obvious thing to want to do.

1
The link contains the code to do this in Xaml. But if you still want to do it in your backend code, use X:Name to add an id to your Label. - Akash Amin
I cannot see the code to do the font size in XAML ? I need it in App.xaml too ? - Welsh King
<Label.FontSize> <OnPlatform x:TypeArguments="x:Double" iOS="10" Android="11" WinPhone="12" /> </Label.FontSize> You can use this in xaml. - Akash Amin
that mentions different platforms, I need iphone 4, iphone 5, ipad 2 etc. - Welsh King
Well to do that you will probably need to check the width and height of the screen to determine the phone. I answered a similar question here : stackoverflow.com/questions/38096347/… ... - Akash Amin

1 Answers

1
votes

You can use Xamarin forms NamedSize Enumeration

You can choose from:

  • Default
  • Large
  • Medium
  • Micro
  • Small

Example:

<Label  FontSize="small"  Text="Joe"></Label>
<Label  FontSize="Large"  Text="Joe"></Label>