Add custom weight other than Bold in iOS Font descriptor in C# xamarin iOS

87 views Asked by At

I have created a custom renderer as i wanted to use "SF Pro Display" font and using UIFont.SystemFontOfSize(size , weight) i get output as .SFUI-weight .Below is the code i used for custom font -

var descriptor = new UIFontDescriptor().CreateWithFamily("SF Pro Display");
            if ((attributes & FontAttributes.Bold) != 0)
        {
            var traits = (UIFontDescriptorSymbolicTraits) 0;
            traits = traits | UIFontDescriptorSymbolicTraits.Bold;

            descriptor = descriptor.CreateWithTraits(traits);
            Control.Font = UIFont.FromDescriptor(descriptor, (nfloat) label.FontSize);
            
        }

Here the problem is FontAttributes and UIFontDescriptorSymbolicTraits only provide options as Bold,Italic and i wanted Semibold ,Medium ,regular also .I have gone through some similar question but solution are in Swift and those in-build methods are not in C#.By above code i get output SFProDisplay-Bold similarly i want for SFProDisplay-Medium etc.

Went through this below links, How can I get weight of a UILabel? iOS - How to detect whether a font is bold/black/heavy...?

1

There are 1 answers

0
Liqun Shen-MSFT On

Actually, I cannnot find addingAttributes method for FontDescriptor. So you may try the following code as an alternative:

var systemFont = UIFont.SystemFontOfSize(24, UIFontWeight.Semibold);
var descriptor = systemFont.FontDescriptor.CreateWithFamily("SF Pro Display");
Control.Font = UIFont.FromDescriptor(descriptor, 24);

Hope it works for you.