Font Adjuster



-->

By default, Xamarin.Forms uses a system font defined by each platform. However, controls that display text define properties that you can use to change this font:

  • FontAttributes, of type FontAttributes, which is an enumeration with three members: None, Bold, and Italic. The default value of this property is None.
  • FontSize, of type double.
  • FontFamily, of type string.

These properties are backed by BindableProperty objects, which means that they can be targets of data bindings, and styled.

The idea behind font adjuster is to adjust the names of old and strange named fonts, especially use the 'new' preferred family and preferred subfamily names to put a whole font family under one single family name. The font-size-adjust CSS property sets the size of lower-case letters relative to the current font size (which defines the size of upper-case letters). /. Use the specified font size./ font-size-adjust: none; /. Use a font size that makes lowercase letters half the specified font size./ font-size-adjust: 0.5; /. Global values./ font-size-adjust: inherit; font-size-adjust: initial; font-size.

To change the font size, we recommend using the zoom function as changes are quick and easy to edit or revert. It also works with all types of content. The following sections show you how to quickly adjust the size of both font and images on a page, or the font only, keeping images their default size.

Set font attributes

Controls that display text can set the FontAttributes property to specify font attributes:

The equivalent C# code is:

Set the font size

Controls that display text can set the FontSize property to specify the font size. The FontSize property can be set to a double value directly, or by a NamedSize enumeration value:

Font

The equivalent C# code is:

Alternatively, the Device.GetNamedSize method has an override that specifies the second argument as an Element:

Note

The FontSize value, when specified as a double, is measured in device-independent units. For more information, see Units of Measurement.

For more information about named font sizes, see Understand named font sizes.

Set the font family

Controls that display text can set the FontFamily property to a font family name, such as 'Times Roman'. However, this will only work if that font family is supported on the particular platform.

There are a number of techniques that can be used to attempt to derive the fonts that are available on a platform. However, the presence of a TTF (True Type Format) font file does not necessarily imply a font family, and TTFs are often included that are not intended for use in applications. In addition, the fonts installed on a platform can change with platform version. Therefore, the most reliable approach for specifying a font family is to use a custom font.

Font Adjuster

Custom fonts can be added to your Xamarin.Forms shared project and consumed by platform projects without any additional work. The process for accomplishing this is as follows:

  1. Add the font to your Xamarin.Forms shared project as an embedded resource (Build Action: EmbeddedResource).
  2. Register the font file with the assembly, in a file such as AssemblyInfo.cs, using the ExportFont attribute. An optional alias can also be specified.

The following example shows the Lobster-Regular font being registered with the assembly, along with an alias:

Note

Font Adjustment

The font can reside in any folder in the shared project, without having to specify the folder name when registering the font with the assembly.

On Windows, the font file name and font name may be different. To discover the font name on Windows, right-click the .ttf file and select Preview. The font name can then be determined from the preview window.

The font can then be consumed on each platform by referencing its name, without the file extension:

Alternatively, it can be consumed on each platform by referencing its alias:

The equivalent C# code is:

The following screenshots show the custom font:

Important

For release builds on Windows, ensure the assembly containing the custom font is passed as an argument in the Forms.Init method call. For more information, see Troubleshooting.

Set font properties per platform

The OnPlatform and On classes can be used in XAML to set font properties per platform. The example below sets different font families and sizes on each platform:

The Device.RuntimePlatform property can be used in code to set font properties per platform

For more information about providing platform-specific values, see Provide platform-specific values. For information about the OnPlatform markup extension, see OnPlatform markup extension.

Understand named font sizes

Xamarin.Forms defines fields in the NamedSize enumeration that represent specific font sizes. The following table shows the NamedSize members, and their default sizes on iOS, Android, and the Universal Windows Platform (UWP):

Adjustment
MemberiOSAndroidUWP
Default171414
Micro121015.667
Small141418.667
Medium171722.667
Large222232
Body171614
Header179646
Title282424
Subtitle221620
Caption121212

The size values are measured in device-independent units. For more information, see Units of Measurement.

Note

On iOS and Android, named font sizes will autoscale based on operating system accessibility options. This behavior can be disabled on iOS with a platform-specific. For more information, see Accessibility Scaling for Named Font Sizes on iOS.

Display font icons

Font icons can be displayed by Xamarin.Forms applications by specifying the font icon data in a FontImageSource object. This class, which derives from the ImageSource class, has the following properties:

  • Glyph – the unicode character value of the font icon, specified as a string.
  • Size – a double value that indicates the size, in device-independent units, of the rendered font icon. The default value is 30. In addition, this property can be set to a named font size.
  • FontFamily – a string representing the font family to which the font icon belongs.
  • Color – an optional Color value to be used when displaying the font icon.

This data is used to create a PNG, which can be displayed by any view that can display an ImageSource. This approach permits font icons, such as emojis, to be displayed by multiple views, as opposed to limiting font icon display to a single text presenting view, such as a Label.

Important

Font icons can only currently be specified by their unicode character representation.

The following XAML example has a single font icon being displayed by an Image view:

Font Adjuster Free

This code displays an XBox icon, from the Ionicons font family, in an Image view. Note that while the unicode character for this icon is uf30c, it has to be escaped in XAML and so becomes . The equivalent C# code is:

The following screenshots, from the Bindable Layouts sample, show several font icons being displayed by a bindable layout:

Font Adjusting

Related links