Xamarin Examples: Using Icon Fonts

Nowadays, mobile developers have to work with a large amount of screen resolutions. Earlier applications had to contain a lot of the same pictures with different sizes to make icons display correctly. A few years ago, the icon font toolkit called Font Awesome became very popular. It gives developers an opportunity to escape from the large amount of pictures and flexibly adapt their products to different screen resolutions.

So, let’s start with the creation of the icon font. To do this we will need images in SVG format and a free service. I will use glyphter.com, which allows the use of a lot of different icons for free.

using-icon-fonts-on-xamarin-1

You can create your own icons, or use presets – for example, try icons8.com. There, you can find a big database of icons and download them in the preferred format.

To use the icon font in Xamarin, you should create a simple Label with renders for each platform. Here’s a little tutorial:

It is possible to display a custom font by first ensuring that it is loaded, then referring to it by name using the Xamarin.Forms font methods. Follow the instructions in this blog post:

iOS

  • Add the font file with Build Action: BundleResource
  • Update the Info.plist file (Fonts provided by application, or UIAppFonts, key)
  • Refer to it by name wherever you define a font in Xamarin.Forms!

Code:

new Label {
    Text = "Hello, Forms!",
    FontFamily = Device.OnPlatform (
        "Lobster-Regular",
        null,
        null
    ), // set only for iOS
}

Android

Xamarin.Forms for Android does not currently expose the ability to set the font to a custom font file, so custom renderers are required. When writing the renderer for the Android platform, create a typeface instance that references a custom font file that has been added to the Assets directory of the application (with Build Action: AndroidAsset).

Code:

[assembly: ExportRenderer (typeof (MyLabel), typeof (MyLabelRenderer))]
namespace WorkingWithFonts.Android {
    public class MyLabelRenderer : LabelRenderer {
        protected override void OnElementChanged (ElementChangedEventArgs<Label> e) {
            base.OnElementChanged (e);
            var label = (TextView)Control; // for example
            Typeface font = Typeface.CreateFromAsset (Forms.Context.Assets, "Lobster-Regular.ttf");  // font name specified here
            label.Typeface = font;
        }
    }
}

Windows Phone

Xamarin.Forms for Windows Phone can reference a custom font that has been added to the project by following a specific naming standard. First, add the font file to the /Assets/Fonts/ folder in the application project and set the Build Action:Content. Then use the full path and Font Name separated by a hash (#) as the font name in Xamarin.Forms, as the code snippet below demonstrates:

new Label {
    Text = "Hello, Forms!",
    FontFamily = Device.OnPlatform (
        null,
        null,
        @"\Assets\Fonts\Lobster-Regular.ttf#Lobster-Regular"
                 // Windows Phone will use this custom font
    )
}

Thanks for your attention!

Programming Tasks Assistance from the Experts

It can never hurt to have Xamarin examples, especially if you don’t know how to deal with your assignment on your own. Here is one more of our assignments on the Web about Xamarin.forms that can be really useful.

Our Xamarin examples are one of the best sources of information for students. When you need to deal with your homework, but don’t know how to do it properly, leave it to AssignmentShark.com programming homework service. We are available 24/7 for our customers’ convenience. We have a team of experts who are knowledgeable in different spheres of study. They can deal with assignments of any difficulty level. Our prices are reasonable, but we never compromise on quality. Place your order right now and get professional assignment help!

Leave a Reply

Your email address will not be published. Required fields are marked *

Customer testimonials

Submit your instructions to the experts without charge.