-->
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:
If the font file has a TTF or OTF extension, you can physically copy it over to your PC and it will work there. However, that wouldn't be legal. The font family Avenir is licensed just like your operating system or your apps. Adobe Fonts partners with the world’s leading type foundries to bring thousands of beautiful fonts to designers every day. No need to worry about licensing, and you can use fonts from Adobe Fonts on the web or in desktop applications.
FontAttributes
, of typeFontAttributes
, which is an enumeration with three members:None
,Bold
, andItalic
. The default value of this property isNone
.FontSize
, of typedouble
.FontFamily
, of typestring
.
These properties are backed by BindableProperty
objects, which means that they can be targets of data bindings, and styled.
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:
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.
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:
- Add the font to your Xamarin.Forms shared project as an embedded resource (Build Action: EmbeddedResource).
- 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
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:
Download Avenir Next Font OTF, TTF
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):
Member | iOS | Android | UWP |
---|---|---|---|
Default | 17 | 14 | 14 |
Micro | 12 | 10 | 15.667 |
Small | 14 | 14 | 18.667 |
Medium | 17 | 17 | 22.667 |
Large | 22 | 22 | 32 |
Body | 17 | 16 | 14 |
Header | 17 | 96 | 46 |
Title | 28 | 24 | 24 |
Subtitle | 22 | 16 | 20 |
Caption | 12 | 12 | 12 |
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:
Avenir Font For Android Phones
Glyph
– the unicode character value of the font icon, specified as astring
.Size
– adouble
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
– astring
representing the font family to which the font icon belongs.Color
– an optionalColor
value to be used when displaying the font icon.
See Full List On Fontsnetwork.com
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
.
Avenir Font For Android App
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:
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:
Avenir Next
The following screenshots, from the Bindable Layouts sample, show several font icons being displayed by a bindable layout:
Comments are closed.