2

My designer for our Xamarin Forms app specified Small Caps for a label field. There are several discussions on how to do this in Xamarin for Android, but I can't find anything for iOS.

I know I will need a custom renderer and have found a good discussion here, but am being challenged in converting the Swift code in that article to C# for Xamarin iOS.

Here's that code:

let systemFont = UIFont.systemFont(ofSize: 24.0, weight: UIFontWeightLight)
let smallCapsDesc = systemFont.fontDescriptor.addingAttributes([
    UIFontDescriptorFeatureSettingsAttribute: [
        [
            UIFontFeatureTypeIdentifierKey: kUpperCaseType,
            UIFontFeatureSelectorIdentifierKey: kUpperCaseSmallCapsSelector
        ]
    ]
])
let font = UIFont(descriptor: smallCapsDesc, size: systemFont.pointSize)

I know that I will put something into my OnElementChanged method, but the FontDescriptor does not have an addingAttributes method or an Attributes collection, so am challenged in adding additional properties.

Wolfgang Schreurs
  • 11,779
  • 7
  • 51
  • 92
Steve Reed Sr
  • 2,011
  • 3
  • 18
  • 22

1 Answers1

2

the FontDescriptor does not have an addingAttributes method or an Attributes collection

You are looking for the UIFont.FontDescriptor.CreateWithAttributes method.

Example:

var systemFont = UIFont.SystemFontOfSize(24, UIFontWeight.Light);
var attributes = new UIFontAttributes(
    new UIFontFeature(CTFontFeatureUpperCase.Selector.UpperCaseSmallCaps),
    new UIFontFeature(CTFontFeatureLowerCase.Selector.LowerCaseSmallCaps)
);
var smallCapsDesc = systemFont.FontDescriptor.CreateWithAttributes(attributes);
var font = UIFont.FromDescriptor(smallCapsDesc, systemFont.PointSize);
Community
  • 1
  • 1
SushiHangover
  • 73,120
  • 10
  • 106
  • 165
  • Works fine with SystemFontOfSize. I tried it with the Custom Font used by the app (Futura LT Medium) and it did not work. I did this by replacing the systemFont = ... with systemFont = Control.Font;. Not sure why it didn't work, but I'm going with what works! Thanks! – Steve Reed Sr Jan 17 '18 at 01:34
  • @SteveReedSr Not all fonts are enabled with the small cap Open Type attributes and personally I'm not sure about Futura LT Medium (3rd party font?). I would try a different font just to check your code (try https://fonts.google.com/specimen/Roboto as I know it works, `var font = UIFont.FromName(@"Roboto", 20);` and then apply the small cap UIFontAttributes to it) and if that does not display in small caps, post another question... – SushiHangover Jan 17 '18 at 02:09