Home > Blockchain >  Can you change the textcolor of all the tools in your project?
Can you change the textcolor of all the tools in your project?

Time:12-21

can you change the text color of all your text inside your project? I mean not by binding or what, just by setting the default color or do I really need to change the property of each label/entry/button etc...?

CodePudding user response:

In Xamarin you can create a global style. From the documentation:

Styles can be made available globally by adding them to the application's resource dictionary. This helps to avoid duplication of styles across pages or controls.

CodePudding user response:

One way is to use the style and target to the label/entry/button etc.

<Style TargetType="Label">
<Setter Property="TextColor" Value="Black" />
</Style>

For more details, refer to: https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/styles/xaml/

Another way is to set the color as resource.

Set the resource:

 <Application.Resources>
    <!-- Colors -->
    <Color x:Key="NormalTextColor">Black</Color>
</Application.Resources>

Usage:

   <Label Text="Hello"
                   TextColor="{StaticResource NormalTextColor}"
                   FontAttributes="Bold" />

For more details, refer to: https://docs.microsoft.com/en-us/xamarin/xamarin-forms/xaml/resource-dictionaries

CodePudding user response:

Like TheTanic answer. For example:

In App.xaml a Style for Label with the name BLabel.

<Style x:Key="BLabel" TargetType="Label">
    <Setter Property="TextColor"  Value="#A7ADB1" />
    <Setter Property="HorizontalOptions" Value="Start" />
    <Setter Property="VerticalOptions" Value="Center" />
</Style>

You can use it like this , in MainPage.xaml.

 <Label
   Grid.Row="4"
   Grid.Column="1"
   Style="{StaticResource BLabel}"
   Text="BB 3" />

But there is more you can add to this like:

 <Setter Property="WidthRequest" Value="150" />
    <Setter Property="HeightRequest" Value="40" />
    <Setter Property="FontSize" Value="Small" />
    <Setter Property="BorderWidth" Value="1" />
    <Setter Property="BackgroundColor" Value="Red" />
    <Setter Property="HorizontalOptions" Value="Center" />
    <Setter Property="TextTransform" Value="None" />

and more ....

Not only for labels but Buttons etc.

This is an example for StaticResource but you can also use DynamicResource to change color's etc.

https://www.youtube.com/watch?v=Se0yF5JXk70&ab_channel=JamesMontemagno

  • Related