How can I set a font system wide in Xamarin.iOS without also setting the font size?

As per the title I am having issues with setting a font across the entire app on iOS without also setting the font size.

I would have imaged this could be achieved through the UIAppearance APIs but it seems the font cannot be set independently.

According to:
https://stackoverflow.com/questions/8707082/set-a-default-font-for-whole-ios-app
It seems to be possible but only using extensions or categories which is something I have tried to replicate in C# but haven't managed successfully.

Does anybody know how I would go about achieving this? I would prefer if this could be done through UIAppearance rather than subclassing UILabel as this will create a lot of extra work!

Answers

  • RodrigoLiraRodrigoLira USMember ✭✭

    If you're using the designer, opening the list of fonts will also bring the list of font styles.

    For example: Body, Caption1, Title 1, Title 2 and so on.

    You can also set via code like this:

    MyLabel.Font = UIFont.PreferredBody; //other styles are there aswell

    Or you can try setting it globally on your AppDelegate:

    UILabel.Appearance.Font = UIFont.PreferredBody;

    By using these styles your text will scale according to iOS font size scale.

  • JayHoward.8199JayHoward.8199 USMember

    Hi Rodrigo, thanks for the suggestion but I am not sure this will work for our scenario as we need to use custom fonts! I can see the UIFontMetrics class but this is only in beta in iOS 11 so it also won't work for us either.

  • RodrigoLiraRodrigoLira USMember ✭✭
    edited October 2017

    Sorry for the delay replying. Hopefully it can still help someone.

    I've implemented with custom fonts. You can follow any instructions on how to embed your custom fonts on the project and then you can use it like this:

    public static UIFont GenerateFont(string name, UIFontTextStyle style) {
        // This will load default system font with the dynamic size
        var descriptor = UIFont.GetPreferredFontForTextStyle(style);
        // Then you'll load your custom font with the dynamic size
        return UIFont.FromName(name, descriptor.PointSize);
    }
    
Sign In or Register to comment.