Add a stylish custom font in Xamarin.Forms

maxresdefault (2)

Although we are reading or writing something on a page or on the web server based blog, a nicely written line in beautiful handwriting or an allure and easily redable font wins the heart every time. These days a cross-platform app development platform Xamarin is ruling the tech industry, and due to the popularity of this application development environment tech gigs mostly prefers it in a way to the building of an features mobile application. Many of time it has been noticed that a wide count of xamarin app developers are looking to add and pick a beautiful stylish font a app that they are creating in Xamarin.Forms.

Here we are going to enemurating the process of adding custom font in Xamarin.Forms.

It requires a font file in your app, that means we need to add the font file in our app, and in case if we do not have the file of that particular font then simply we can download it from the Google. Basically Xamarin supports these three file extensions:

  • TTF – True Type Font
  • OTF — Open Type Font
  • WOFF – Web Open Font Format

Now follow the below steps to add a custom font with the live example of this simple app.

Create a new Xamarin app and add the file of the ofnt of your’s choice in your projcet. Both Android and iOS supports the same font file. After adding the file must check and doone thing by right clicking on the file and go onto the Build Action tab and check carefully it should be checked at Bundle Resource in iOS and Android Asset in Android. Now these files are ready to use, and its time to check the code.

For this add this code in your xaml file:

<StackLayout>

<Label Text=”My stylish font in xamarin forms” HorizontalOptions=”CenterAndExpand” VerticalOption=” CenterAndExpand”>

</StackLayout>

If you want to add the same font style i

n your entire application, you need to use all the controls of a application such as Code, Entry, Button, Editor and so on. For this intention use the below code:

If you are using Android:

using System;

using android.Graphics;

using TestCustomFont.Droid.Renderers;

using Xamarin.Forms;

using Xamarin.Forms.Platform.Android;

[assembly: ExportRenderer(typeof(Label), typeof(EntendedLabelRenderer))] namespace TestCustomFont.Droid.Renderers

{

Public class ExtendedlabelRenderer: LabelRenderer

{

Protected override void OnElementChanged(ElementChangedEventArgs<Label>e)

{

Base.OnElementChanged(e);

Control.Typeface = Typeface.CreateFormAsset(Forms.Context.Assets, “GreatVibes-Regular.otf”);

Control.TextSize = 30;

}

}

}

Use this code for iOS:

using System;

using TestCustomFont.iOS.Renderers;

using Xamarin.Forms;

using Xamarin.Forms.Platform.iOS;

[assembly: ExportRenderer(typeof(Label), typeof(ExtendedLabelRenderer))]

namespace TestCustomFont.iOS.Renderers

{

public class ExtendedLabelRenderer: LabelRenderer

{

protected override void OnElementChanged(ElementChangedEventArgs<Label> e)

{

base.OnElementChanged(e);

Control.Font = UIKit.UIFont.FromName(“GreatVibes-Regular”, (System.nfloat)e.NewElement.FontSize);

}

}

}

That’s all to add a stylish font in Xamarin.Forns, use the code and enjoy running it.

For development details and further discussions, please contact us on the below mentioned details:

Website Address: http://hire-xamarin-developer.com/
Email: info@hire-xamarin-developer.com
Call: +1-9796334727 , +91-9229225552

by admin

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>