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.