Computer Magic Logo
Font

Monday, April 11, 2016

Published by Aristotelis Pitaridis

The font defines how the text will appear on controls like labels and buttons. In order to define the appearance of the text we can use the following properties.

  • FontFamily of type string
  • FontSize of type double
  • FontAttributes of type FontAttributes

The FontFamily can take a value like "Times Roman" but we have to be careful because each operating system has different font names. This means that we have to use it in connection with Device.OnPlatform in order to define a font name for each platform. Below we cam see am example of how to do it.

MyLable.FontFamily = Device.OnPlatform("MarkerFelt-Thin", "Droid Sans Mono", "Comic Sans MS");

For the FontSize we have a similar problem so we can use the GetNamedSize in order to get a proper value for the font size.

MyLable.FontSize = Device.GetNamedSize(NamedSize.Large, typeof(Label));

The Device.GetNamedSize gets the name of the size and the type of the control which will be used in order to display the text. The name of the size can have one of the following values.

  • Default
  • Micro
  • Small
  • Medium
  • Large

The FontAttributes allow us to define if the text is going to be bold or italic. Below we can see an example of setting the FontAttributes property.

MyLable.FontAttributes = FontAttributes.Bold | FontAttributes.Italic;