development development .

development

Different types of font resources in Xamarin and how to use them

In order for the operating system to support these fonts and show exactly what is in the application , it is necessary to apply special settings to support the fonts in the application. In this tutorial, we will teach you how to apply settings to the operating system to support application fonts. If you want to know about them, follow this tutorial.


The ways to use fonts in operating systems and applications are as follows:
1- One way to use fonts and display them is to package all fonts as the source of Android 2- and put them in the application and operating system. In this case, it is clear that all fonts are always present in the Android operating system, and when needed, you can refer to its source. Packing fonts and placing them on the Android source allows us to access the fonts at all times.

3- The second way to use fonts is to download fonts. In this case, the Android operating system also supports the download of fonts, so that if necessary, we download the relevant font and save it inside the Android operating system. Fonts are downloaded by the font provider and the font provider checks to see if the corresponding font is present within the operating system. If there is no relevant font, the font provider will download it and save it inside the Android operating system.

It should be noted that all fonts that are similar or similar and have the same style are classified in one group. Fonts that have the same style and are similar to each other are grouped into groups such as font families. Users must consider unique specifications in order to use these similar fonts. In this way, they can choose their unique names or specify their characteristics. Android Support Library v26 uses API level 26 to support and use fonts. If we consider APIs whose level is less than 26, then we have to specify android: namespace and app: namespace for them. If we do not specify android: namespace and app : namespace for them and consider only android: namespace, they may not be displayed on lower than 26 operating systems. So in order for all fonts to be displayed with an API level less than 26, both android: namespace and app: namespace must be specified, otherwise if not specified, both fonts will not be displayed. APIs with a level below 26 do not support fonts and must be set to android: namespace and app: namespace in order to be displayed.

The code snippets below are used to display the font family in API level 14 and above.


xmlns: android = "http://schemas.android.com/apk/res/android"
xmlns: app = "http://schemas.android.com/apk/res-auto">
android: fontStyle = "normal"
android: fontWeight = "400"
app: font = "@ font / sourcesanspro_regular"
app: fontStyle = "normal"
app: fontWeight = "400" />

To display different types of fonts in TextView, you need to make the appropriate settings. For example, the following code snippet describes how to display different types of fonts in TextView.

< TextView
android: text = "The quick brown fox jumped over the lazy dog."
android: fontFamily = "@ font / caveat_bold"
app: fontFamily = "@ font / caveat_bold"
android: textAppearance = "? android: attr / textAppearanceLarge"
android: layout_width = "match_parent"
android: layout_height = "wrap_content" />


Source of Font Package in Android
If the fonts are in the form of Packages, it means that the fonts are guaranteed to be inside the operating system and can always be used. Font files in TTF or TOF format like other programs are added to Xamarin. Android and stored in the operating system. The font files are located in the Resources folder of the Xamarin.Android project.

Note:
Fonts must have Built Action from AndroidResource.

Fonts in a family and group
Fonts that have the same style and are similar to each other are placed in a group called Font Families. The font family is saved with the font elements in the XML file. The XML file is also stored in the Resources / font path.

Each font family must have its own XML file to be saved with its fonts and elements. If you want to create a font family, you must add all the fonts to the Resources / font folder. Then create an XML file inside the created font folder.

The XML file creates a font family below and stores them in the Resources / font folder named sourcesanspro.xml.


xmlns: app = "http://schemas.android.com/apk/res-auto">
android: fontStyle = "normal"
android: fontWeight = "400"
app: font = "@ font / sourcesanspro_"
app: fontStyle = "normal"
app: fontWeight = "400" />
android: fontStyle = "normal"
android: fontWeight = "800"
app: font = "@ font / sourcesanspro_bold"
app: fontStyle = "normal"
app: fontWeight = "800" />
android: fontStyle = "italic"
android: fontWeight = "400"
app: font = "@ font / sourcesanspro_italic"
app: fontStyle = "italic"
app: fontWeight = "400" />


Three different values ​​can be given for the fontStyle attribute:
- normal
- Italic
- bold
The fontWeight feature is used to specify the font thickness and can take different values:
- Thin - 100
- Extra Light - 200
- Light - 300
- Normal - 400
- Medium - 500
- Semi Bold - 600
- Bold - 700
- Extra Bold - 800
- Black - 900
The code snippet below defines a family of fonts with Italic text style features and a thickness of 400:
android: text = "Sans Source Pro semi-bold italic, 600 weight, italic"
android: layout_width = "match_parent"
android: layout_height = "wrap_content"
android: fontFamily = "@ font / sourcesanspro"
android: textAppearance = "? android: attr / textAppearanceLarge"
android: gravity = "center_horizontal"
android: fontWeight = "600"
android: textStyle = "italic"
/>


Provide custom font settings with programming
The code below shows how programming can be used to display fonts inside TextView:
Android.Graphics. Typeface = this. Resources.GetFont (Resource.Font. caveat_regular);
textView1.Typeface = typeface;
textView1.Text = "Changed the font";

If you use the GetFont method, it automatically selects the first font in the font family inside the folder, but if you want to choose a font with special features, you can use the Typeface.Create method. This method provides you with the fonts you want.

Selects the code below the font whose Typeface is bold for display in TextView.

var typeface = Typeface.Create ("", Android.Graphics. TypefaceStyle.Bold);
textView1.Typeface = typeface;


Download fonts
Instead of using packaged fonts and storing them inside the operating system, you can download the fonts and temporarily save the audio inside the operating system or application . In this case, you also reduce the size of the APK. If you use packaged fonts, the size of the APK will increase, but if you download the fonts from various sources and temporarily store them in applications and operating systems, their size will decrease.

Fonts are downloaded from various sources by the font provider. Android operating system with version 8.0 has a font provider for downloading fonts from various sources that download fonts from this version from Google Font Repository.

How the font provider works is that after requesting a program or application for a font with special features and specifications, the font provider checks whether there are fonts with the same features and specifications in the system. If there is no font with the same features and specifications, the font provider tries to download the font from different sources and submit it to the program and application. After downloading the font from a source, in addition to the program and application request Other programs and applications can also use the downloaded font, although it should be noted that applications do not request the font directly from the font provider, but applications and applications will use the FontsContract API.

How to support Android operating system with version 8.0 of downloading fonts from various sources:
1- Announcing downloadable fonts for the Android operating system as a source: In this case, the program announces the downloadable fonts as XML resource files. Android saves these fonts on the system after downloading and integrating them.

Programming:
A FontRequest is considered for each font and FontRequest is sent to the FontsContract class. FontsContract actually takes FontRequest and then receives the desired font from the font provider.

2- resources files must be added to the Xamarin.Android project and application before the fonts are downloaded. First, the fonts are placed inside the XML file in the Resources / font folder.

The code below shows how to download the fonts in full:

xmlns: app = "http://schemas.android.com/apk/res-auto"
android: fontProviderAuthority = "com.google.android.gms. fonts"
android: fontProviderPackage = "com.google.android.gms"
android: fontProviderQuery = "VT323"
android: fontProviderCerts = "@ array / com_google_android_gms_fonts_certs"
app: fontProviderAuthority = "com.google.android.gms. fonts"
app: fontProviderPackage = "com.google.android.gms"
app: fontProviderQuery = "VT323"
app: fontProviderCerts = "@ array / com_google_android_gms_fonts_certs"
>


The information that Android needs to receive and download fonts is as follows:
fontProviderAuthority:
Allows the Font Provider to check and use the request.

fontQuery:
is actually a string that helps the font provider to find the desired font.

fontProviderCerts:
is a resource presentation.

Font Certificates
If the font provider is not already installed on the device, the Android operating system will need some font provider security certificates. These certificates are stored inside the resource array file in the Resources / values ​​directory and path.

For example, the code below is an XML file that stores Google font provider certificates.




@ array / com_google_android_gms_fonts_certs_dev
@ array / com_google_android_gms_fonts_certs_prod









Despite these source files, fonts can be downloaded and used.
Declare downloadable fonts as the source of the fonts
If we list the downloadable fonts inside the AndroidManifest.XML file, Android will start downloading them asynchronously.



@ font / vt323


After these codes, the following code must be added to them:

Download fonts using the Font API for use in applications installed on the Android operating system
If you use the FontContractCompat.RequestFont method to download fonts, this method will first check if the requested font is present within the system itself. If the desired font is not inside the system, the font provider will start searching for that font from various sources and finally download it and use it inside the application and system. FontRequest is the same request that is sent to the font provider and requests the download of the font with the desired specifications.

If the desired font does not exist inside the system and the font provider cannot download it, then the default font of the Android operating system will be used.


FontRequest has four sections:
Font Provider Authority: This part of FontRequest allows the Font Provider to use the request and the Font Provider downloads the desired font and places it in the system according to the request.

Package:
Font Provider is used to authenticate and actually allows it to use the request.

fontQuery:
This section is a string that contains details and information about fonts.

fontProviderCerts:
This section is also a resource presentation.

The following code describes FontRequest simulation:
FontRequest request = new FontRequest ("com.google.android.gms. fonts", "com.google.android.gms", , Resource.Array.com_google_android_gms_fonts_certs);

The code below also indirectly downloads the font from the Google Fonts Open Source collection.

public class FontDownloadHelper: FontsContractCompat.FontRequestCallback
{
// A very simple font query; replace as necessary
public static readonly String FontToDownload = "Courgette";

Android.OS. Handler = null;

public event EventHandler FontDownloaded = delegate
{
// just an empty delegate to avoid null reference exceptions.
};


public void DownloadFonts (Context context)
{
FontRequest request = new FontRequest ("com.google.android.gms. fonts", "com.google.android.gms", FontToDownload, Resource.Array.com_google_android_gms_fonts_certs);
FontsContractCompat.RequestFont (context, request, this, GetHandlerThreadHandler ());
}

public override void OnTypefaceRequestFailed (int reason)
{
base. OnTypefaceRequestFailed (reason);
FontDownloaded (this, new FontDownloadEventArg (null));
}

public override void OnTypefaceRetrieved (Android.Graphics. Typeface typeface)
{
base. OnTypefaceRetrieved (typeface);
FontDownloaded (this, new FontDownloadEventArg (typeface));
}

Handler GetHandlerThreadHandler ()
{
if (Handler == null)
{
HandlerThread = new HandlerThread ("fonts");
handlerThread.Start ();
Handler = new Handler (handlerThread.Looper);
}
return Handler;
}
}


///


/// EventArg when a font has been downloaded.
///
public class FontDownloadEventArg: EventArgs
{
public FontDownloadEventArg (Android.Graphics. Typeface typeface)
{
Typeface = typeface;
}
public Android.Graphics. Typeface {get; private set;}
public bool RequestFailed
{
get
{
return Typeface! = null;
}
}
}
The code below also creates a FontDownloadHelper that will guide and assist you in the font download process.
var fontHelper = new FontDownloadHelper ();

fontHelper.FontDownloaded + = (object sender, FontDownloadEventArg e) =>
{
// React to the request
};
fontHelper.DownloadFonts (this); // this is an Android Context instance.


In this tutorial, you will get acquainted with the APIs used in version 8.0 of Android. How to place fonts inside the APK and use and display them in the layout in this tutorial, was taught. You can apply special settings using the codes included in this tutorial and you can use different fonts by using two methods of programming and downloading fonts.

Source:https://www.dotnek.com/Blog/Apps/different-types-of-font-resources-in-xamarin


برچسب: ، app، application، mobileapp، mobileapplication، mobile، DotNek، monetizing، development، programming، developer، Xamarin، android، Ios، revenue،
امتیاز دهید:
رتبه از پنج: 0
بازدید:

+ نوشته شده: ۳۰ خرداد ۱۴۰۰ساعت: ۱۲:۵۱:۳۲ توسط:Media موضوع: نظرات (0)