See Also: UIFont Members
UIFont object are not instantiated directly, but instead are created from static methods on this class. Starting with iOS 7.0 you can use the UIFont.PreferredBody, UIFont.PreferredCaption1, UIFont.PreferredCaption2, UIFont.PreferredHeadline, UIFont.PreferredSubheadline properties to get the preferred system fonts for the corresponding use case.
Use the static UIFont.FromName method to create new fonts of a particular font.
You can use the UIFont.FamilyNames property to get a list of all family names available and then the UIFont.FontNamesForFamilyName(string) method to get a list of the fonts available in a particular font family.
To get a normal, italic of bold fonts representing the system font, use the static methods UIFont.SystemFontOfSize, UIFont.ItalicSystemFontOfSize and UIFont.BoldSystemFontOfSize.
You can query some common font parameters by using the UIFont.ButtonFontSize, UIFont.LabelFontSize, UIFont.SmallSystemFontSize and UIFont.SystemFontSize.
Starting with iOS7, you can retrieve a new font based on an existing font instance by using font descriptors. To do this, you fetch the immutable UIFont.FontDescriptor property from your font and use it to create a new font descriptor with your desired changes, which then you use to can pass to the UIFont.FromDescriptor method to create the altered font.
The following example shows how to alter the existing font to set enable the CoreText typographic features for proportional numbers and to use the character alternative:
C# Example
UIFont CustomizeFont (UIFont font) { var originalDescriptor = font.FontDescriptor; // This font has a character alternative, at index 1, use that: const int characterAlternative = 1; var attributes = new UIFontAttributes ( new UIFontFeature (CTFontFeatureNumberSpacing.Selector.ProportionalNumbers), new UIFontFeature (characterAlternative)); var newDesc = originalDescriptor.CreateWithAttributes (attributes); return UIFont.FromDescriptor (newDesc, 80); }
The following example alters a font descriptor by altering the character set supported by the font and forces the system to use a glyph from the list of fallback fonts in the system. For example, if you were to use the Menlo font, the following example would remove the use of the Melon Snowman character (At Unicode 0x2603), and fall back to the system snowman:
C# Example
var label = new UILabel (new RectangleF (0, 300, 200, 100)) { Text = "Hello \u2603 there!", TextColor = UIColor.White, Font = MyFonts.Menlo }; var originalDescriptor = label.Font.FontDescriptor; var originalCharSet = originalDescriptor.CharacterSet; // Make a copy of the character set to modify var charSetCopy = originalCharSet.MutableCopy () as NSMutableCharacterSet; // Remove the snowman charSetCopy.RemoveCharacters (new NSRange (0x2603, 1)); // Create new descriptor var removedSnowmanDescriptor = originalDescriptor.CreateWithAttributes (new UIFontAttributes () { CharacterSet = originalCharSet }); // Create the font and set it on our label label.Font = UIFont.FromDescriptor (removedSnowmanDescriptor, 0);
The members of this class can be used from a background thread.