0

I am currently trying to change the font for my Xamarin.Forms application by creating a custom renderer for all of the labels within my app. There seems to be an issue that in some locations the text is changing to the correct font but in the majority of the app the labels seem to lose their text and display no text at all.

I am creating a Xamarin.Forms app which will use a Gotham-Book font for text with no font attributes and a Gotham-Medium font for text that has a font attribute of bold. Both fonts are .TTF and are in a Fonts folder inside the Resources folder.

using System;
using System.ComponentModel;
using DemoApp.Controls;
using DemoApp.iOS.Renderers;
using Foundation;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;

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

namespace DemoApp.iOS.Renderers
{
    public class CustomLabelRenderer : LabelRenderer
    {
        private bool _disposed;

        protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
        {
            try
            {
                base.OnElementChanged(e);

                if (Control != null)
                {
                    var sizeToSet = GetNewFontSize();

                    if (Element.FontAttributes == FontAttributes.Bold)
                    {
                        Control.Font = UIFont.FromName("Gotham-Medium", (float)sizeToSet);
                    }
                    else
                    {
                        Control.Font = UIFont.FromName("Gotham-Book", (float)sizeToSet);
                    }
                }
            }
            catch (Exception ex)
            {
                //Log
            }
        }
    }
}

I expect the app to change to the new Gotham fonts throughout the entire app, but the majority of the applications labels lose their text and display nothing.

  • In addition to this, on iOS you have to open up your info.plist, and add in the file names of the fonts you want to use. – Lucas Zhang Jul 15 '19 at 10:47
  • @LucasZhang-MSFT I already have this in my info.plist ` Fonts/GothamBook.ttf Fonts/GothamMedium.ttf` – Caolán McKeown Jul 15 '19 at 11:02
  • UIAppFonts GothamBook.ttf GothamMedium.ttf – Lucas Zhang Jul 15 '19 at 11:05
  • Check https://xamarinhelp.com/custom-fonts-xamarin-forms/ – Lucas Zhang Jul 15 '19 at 11:05
  • @LucasZhang-MSFT My application already has this in the info.plist `UIAppFonts Fonts/GothamBook.ttf Fonts/GothamMedium.ttf ` and needs to keep in "Fonts/" in the location as the fonts are located within a subfolder called Fonts within the Resources folder – Caolán McKeown Jul 15 '19 at 11:11
  • Could you share two ttf files so that I can test it on my side? – Lucas Zhang Jul 15 '19 at 11:13
  • @LucasZhang-MSFT Here are the two files https://www.dropbox.com/sh/l2irf7j90v7wlyd/AABZC9bsdd24kxiv3474QcC5a?dl=0 – Caolán McKeown Jul 15 '19 at 11:28
  • It works fine on my side . What's the means of ` but in the majority of the app the labels seem to lose their text and display no text at all` You can share a sample which contains the issue so that I can test it . – Lucas Zhang Jul 15 '19 at 13:00
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/196514/discussion-between-caolan-mckeown-and-lucas-zhang-msft). – Caolán McKeown Jul 16 '19 at 07:50

0 Answers0