iosxamarinxamarin.iosdynamictype

Xamarin IOs, What are the string values to pass into UIFontMetrics.GetMetrics method for the textStyle parameter?


I'm having a Xamarin.IOs project. I need to use dynamic type fonts in my application.

I have followed this tutorial in doing so. The tutorial is in in swift I guess. And for the code segment,

label.Font = UIFontMetrics(forTextStyle: .title1).ScaledFont(for: customFont)

in the tutorial, the equivalent Xamarin.IOs C# code is,

label.Font = UIFontMetrics.GetMetrics(textStyle: <string_value>).GetScaledFont(customFont);

But what are the values can I use for <string_value> of textStyle?

I have tried,

but non helped.

What string values does UIFontMetrics.GetMetrics() accept?


Solution

  • There are two ways to do this,

    Way 1:

    UIFontMetrics.GetMetrics(UIFontDescriptor.PreferredTitle1.TextStyle).GetScaledFont(customFont);
    

    The text style's string values can be taken from the TextStyle property of UIFontDescriptor text style properties,

    UIFontDescriptor.PreferredTitle1.TextStyle
    

    Way 2:

    UIFontMetrics.GetMetrics(UIFontTextStyle.Title1.GetConstant()).GetScaledFont(customFont);
    

    The text style's string values can be taken from the GetConstant() extension method of UIFontTextStyle text style properties,

    UIFontTextStyle.Title1.GetConstant()
    

    One of these would be much better than entering string values directly 😊