iOS Guides | Samples

MonoTouch.UIKit.UIFont Class

Font representation for MonoTouch.UIKit classes.

See Also: UIFont

Syntax

[MonoTouch.Foundation.Register("UIFont", true)]
public class UIFont : NSObject, INSCopying, IDisposable

Remarks

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.

System Information and Fonts

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.

Activating Typographic Features

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);
}

Limiting Character Coverage of a Font

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.

Related content

Requirements

Namespace: MonoTouch.UIKit
Assembly: monotouch (in monotouch.dll)
Assembly Versions: 0.0.0.0

The members of MonoTouch.UIKit.UIFont are listed below.

See Also: NSObject

Public Constructors

Default constructor that initializes a new instance of this class with no parameters.
A constructor that initializes the object from the data stored in the unarchiver object.
Constructor to call on derived classes to skip initialization and merely allocate the object.
A constructor used when creating managed representations of unmanaged objects; Called by the runtime.

Public Properties

[read-only]
AscenderSingle. The top y-coordinate of the longest ascender, as measured in points from the baseline.
[read-only]
static
ButtonFontSizeSingle. Returns the font size of a standard button in points.
[read-only]
CapHeightSingle. The height of a capital letter, measured in points. Read-only.
[read-only]
override
ClassHandleIntPtr. The handle for this class.
[read-only]
DescenderSingle. The bottom y-coordinate of the longest descender, as measured from the baseline, in points.
[read-only]
FamilyNameString. The name of the font family that identifies one (or more) specific fonts. Read-only.
[read-only]
static
FamilyNamesString[]. A list of the font families on the system.
[read-only]
FontDescriptorUIFontDescriptor. The UIFontDescriptor for this UIFont.
[read-only]
static
LabelFontSizeSingle. This property will return the size of the font use for labels, as measured in points.
[read-only]
LeadingSingle. Deprecated: spacing between lines of text from baseline to baseline. Use LineHeight instead.
[read-only]
LineHeightSingle. The height of the text lines as measured in points. Read-only.
[read-only]
NameString. The name of the font. Read-only.
[read-only]
PointSizeSingle. The size of a point in the receiver.
[read-only]
static
PreferredBodyUIFont. The preferred UIFont for the Body dynamic type.
[read-only]
static
PreferredCalloutUIFont. The preferred UIFont for the Callout dynamic type.
[read-only]
static
PreferredCaption1UIFont. The preferred UIFont for the Caption1 dynamic type.
[read-only]
static
PreferredCaption2UIFont. The preferred UIFont for the Caption2 dynamic type.
[read-only]
static
PreferredFootnoteUIFont. The preferred UIFont for the Footnote dynamic type.
[read-only]
static
PreferredHeadlineUIFont. The preferred UIFont for the Headline dynamic type.
[read-only]
static
PreferredSubheadlineUIFont. The preferred UIFont for the Subheadline dynamic type.
[read-only]
static
PreferredTitle1UIFont. The preferred UIFont for the Title1 dynamic type.
[read-only]
static
PreferredTitle2UIFont. The preferred UIFont for the Title2 dynamic type.
[read-only]
static
PreferredTitle3UIFont. The preferred UIFont for the Title3 dynamic type.
[read-only]
static
SmallSystemFontSizeSingle. The size of the system font as measured in points.
[read-only]
static
SystemFontSizeSingle. Creates a system font of the specified size, as measure in points.
[read-only]
xHeightSingle. The size of the a lower cased "x", as measured in points.

Public Methods

static
BoldSystemFontOfSize(Single) : UIFont
Returns a boldfaced font of the standard system font in the size specified.
Copy(NSZone) : NSObject
Performs a copy of the underlying Objective-C object.
override
Equals(Object) : Boolean
Used to compare UIFont objects.
static
FontNamesForFamilyName(String) : String[]
Returns a list of fonts in the given family.
static
FromDescriptor(UIFontDescriptor, Single) : UIFont
Factory method that creates a UIFont from the specified descriptor.
static
FromName(String, Single) : UIFont
Creates a font of the specified size.
override
GetHashCode() : Int32
Generates a hash code for the current instance.
static
GetPreferredFontForTextStyle(NSString) : UIFont
Weakly-typed version of an API used to retrieve the user's desired font size.
static
GetPreferredFontForTextStyle(NSString, UITraitCollection) : UIFont
Weakly-typed version of an API used to retrieve the user's desired font size.
static
ItalicSystemFontOfSize(Single) : UIFont
Creates an italicized system font of the specified size.
static
MonospacedDigitSystemFontOfSize(Single, UIFontWeight) : UIFont
The system monospaced font specialized for digits, in the specified size and weight.
static
MonospacedDigitSystemFontOfSize(Single, Single) : UIFont
The system monospaced font specialized for digits, in the specified size and weight.
static
SystemFontOfSize(Single) : UIFont
Creates a system font of the specified size.
static
SystemFontOfSize(Single, UIFontWeight) : UIFont
Returns the default system font in specified size and weight.
static
SystemFontOfSize(Single, Single) : UIFont
Returns the default system font in specified size and weight.
override
ToString() : String
Returns a string representation of the value of the current instance.
WithSize(Single) : UIFont
Returns a new font based on the current one, with the new specified size.

Public Operators

static
Equality(UIFont, UIFont)Compares two UIFont objects for value equality.
static
Inequality(UIFont, UIFont)Compares two UIFont objects for value inequality.