Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change Font/Color/Size in Xamarin C#

I'm making an Hybrid application in C#/Xamarin, and i want to make a custom menu for all applications(iOS, Android, Windows Phone).

So, I create a MasterPage to be my Menu.

public MasterPage()
{
     InitializeComponent();
     var masterPageItems = new List<MenuItem>();

      masterPageItems.Add(new MenuItem
            {
                Title = "Administração",
            });
            masterPageItems.Add(new MenuItem
            {
                Title = "Meus Dados",
                IconSource = "contacts.png",
                TargetType = typeof(MeusDados),
            });
            masterPageItems.Add(new MenuItem
            {
                Title = "Dados Cadastrais",
                IconSource = "contacts.png",
                TargetType = typeof(MeusNegocios),
            });

     var listView = new ListView
     {
        ItemsSource = masterPageItems,
        ItemTemplate = new DataTemplate(() =>
        {
            var imageCell = new ImageCell();
            imageCell.SetBinding(TextCell.TextProperty, "Title");
            imageCell.SetBinding(ImageCell.ImageSourceProperty, "IconSource");
            return imageCell;
        }),
        VerticalOptions = LayoutOptions.FillAndExpand,
        SeparatorVisibility = SeparatorVisibility.None
     };

    Padding = new Thickness(0, 20, 0, 0);
    Content = new StackLayout
    {
           VerticalOptions = LayoutOptions.Fill,
           Children = {
           listView
           }
     };
}

This is the MenuItem:

public class MenuItem
{
    public string Title { get; set; }

    public string IconSource { get; set; }

    public Type TargetType { get; set; }
    public string Parameter { get; set; }
}

So now I want to change the size of content page, font, font color, font size in C#. How should I do that?

like image 612
Pedro Franco Avatar asked Oct 13 '16 12:10

Pedro Franco


People also ask

How do I change the picker text color in Xamarin?

You can use TextColor and TitleColor to change the font color and HeightRequest to change the size.

How do I change font size in Visual Studio?

On the menu bar, choose Tools > Options. In the options list, choose Environment > Fonts and Colors. In Show settings for list, select Text Editor. Modify the Font and Size options to change the font and text size for the editor.


1 Answers

Xamarin Forms doc on Fonts: Fonts: https://developer.xamarin.com/guides/xamarin-forms/user-interface/text/fonts/

Example:

var about = new Label {
    FontSize = Device.GetNamedSize (NamedSize.Medium, typeof(Label)),
    FontAttributes = FontAttributes.Bold,
    Text = "Medium Bold Font"
};

I do note that you are using an ImageCell, which does not have the Font properties but only a TextColor and DetailColor property. Also there are no properties to get the underlying Labels in the ImageCell, so your best bet if you want full customization is to create your own ViewCell and add the Image and the Labels to the ViewCell. Then you can style your Labels with the Font properties.

Alternately, you can use Themes, which is in Preview: https://developer.xamarin.com/guides/xamarin-forms/themes/

StyleClass The StyleClass property allows a view's appearance to be changed according to a definition provided by a theme.

like image 121
jgoldberger - MSFT Avatar answered Sep 25 '22 17:09

jgoldberger - MSFT