Forum Xamarin.Forms

No easy way to use Custom Font for FormattedString on Android

AlexLauAlexLau HKMember ✭✭
edited March 2015 in Xamarin.Forms

Custom font in Label is simple by using custom renderer but FormattedString is not the case.

Not only I have to read deeply into the assembly of Xamarin Forms to find out the lifecycle behind, but almost every steps I wish to override are all

  • extensions (FormattedString.ToAttributed)
  • private (Label.UpdateText())
  • sealed (Span)

which cannot be overrided.

I end up spending a few hours and re-implementing the whole chain from LabelRenderer to FormattedStringExtensions just to change the implementation of Font.ToTypeface() (Gist). :(

My questions:

  1. Is there workaround which is easier and more elegant?

  2. Are Xamarin Team going to make more classes public or virtual so that even without open sourcing the project, developers can modify some behaviours more easily?


I just found that Text in Span is not BindableProperty AND AGAIN Span is a sealed class :(:(:( Could Xamarin Team make it either non-sealed or bindable so that Span can become a useful class again?



  • BuildCalcBuildCalc USMember ✭✭✭

    I've been struggling with just how hard custom spans and custom fonts are with Xamarin so I built a Label element into the Forms9Patch library to make this much, much easier. It uses HTML markup to simplify customization and has a element where you can insert custom fonts mid-string (or you can just set the entire font family to a custom font). To use a custom font, I add the font file as an Embedded Resource to the applications PCL project and set the Label's FontFamily property (or tag to the font's EmbeddedResourceID.

    new Forms9Patch.Label { 
        HtmlText =  "some plain text <font face=\"Forms9PatchDemo.Resources.Fonts.MaterialIcons-Regular.ttf\"></font> plain"
    new Forms9Patch.Label { 
        HtmlText =  "&lt;Bold&gt;: <b>Bold</b> w/ serif italic serif base font",
        FontFamily = "Serif",
        FontAttributes = FontAttributes.Italic,
        TextColor = Color.Blue,
Sign In or Register to comment.