UIFont Class

Definition

Font representation for UIKit classes.

[Foundation.Register("UIFont", true)]
public class UIFont : Foundation.NSObject, Foundation.INSCopying, Foundation.INSSecureCoding, IDisposable
[<Foundation.Register("UIFont", true)>]
type UIFont = class
    inherit NSObject
    interface INSCoding
    interface INativeObject
    interface IDisposable
    interface INSCopying
    interface INSSecureCoding
Inheritance
UIFont
Attributes
Implements

Remarks

UIFont objects are not instantiated directly, but instead are created from static methods on this class. You can use the PreferredBody, PreferredCaption1, PreferredCaption2, PreferredHeadline, PreferredSubheadline properties to get the preferred system fonts for the corresponding use case.

Use the static FromName(String, NFloat) method to create new fonts of a particular font.

You can use the FamilyNames property to get a list of all family names available and then the 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 or bold font representing the system font, use the static methods SystemFontOfSize(NFloat, UIFontWeight), ItalicSystemFontOfSize(NFloat) and BoldSystemFontOfSize(NFloat).

Activating Typographic Features

You can retrieve a new font based on an existing font instance by using font descriptors. To do this, you fetch the immutable 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 FromDescriptor(UIFontDescriptor, NFloat) method to create the altered font.

The following example shows how to alter the existing font to enable the CoreText typographic features for proportional numbers and to use the character alternative:

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:

var label = new UILabel (new CGRect (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.

Constructors

Name Description
UIFont(NativeHandle)

A constructor used when creating managed representations of unmanaged objects. Called by the runtime.

UIFont(NSCoder)

A constructor that initializes the object from the data stored in the unarchiver object.

UIFont(NSObjectFlag)

Constructor to call on derived classes to skip initialization and merely allocate the object.

Properties

Name Description
AccessibilityAttributedUserInputLabels (Inherited from NSObject)
AccessibilityRespondsToUserInteraction (Inherited from NSObject)
AccessibilityTextualContext (Inherited from NSObject)
AccessibilityUserInputLabels (Inherited from NSObject)
Ascender
ButtonFontSize
CapHeight
Class (Inherited from NSObject)
ClassHandle

The Objective-C class handle for this class.

DebugDescription (Inherited from NSObject)
Descender
Description (Inherited from NSObject)
FamilyName
FamilyNames
FontDescriptor
Handle

Handle (pointer) to the unmanaged object representation.

(Inherited from NSObject)
IsDirectBinding

Gets or sets a value indicating whether this instance uses direct Objective-C binding.

(Inherited from NSObject)
IsProxy (Inherited from NSObject)
LabelFontSize
Leading
LineHeight
Name
PointSize
PreferredBody

The preferred UIFont for the Body text style.

PreferredCallout

The preferred UIFont for the Callout text style.

PreferredCaption1

The preferred UIFont for the Caption1 text style.

PreferredCaption2

The preferred UIFont for the Caption2 text style.

PreferredFootnote

The preferred UIFont for the Footnote text style.

PreferredHeadline

The preferred UIFont for the Headline text style.

PreferredSubheadline

The preferred UIFont for the Subheadline text style.

PreferredTitle1

The preferred UIFont for the Title1 text style.

PreferredTitle2

The preferred UIFont for the Title2 text style.

PreferredTitle3

The preferred UIFont for the Title3 text style.

RetainCount (Inherited from NSObject)
Self (Inherited from NSObject)
SmallSystemFontSize
Superclass (Inherited from NSObject)
SuperHandle

Handle used to represent the methods in the base class for this NSObject.

(Inherited from NSObject)
SystemFontSize
xHeight
Obsolete.

The size of the a lower cased "x", as measured in points.

XHeight
Zone (Inherited from NSObject)

Methods

Name Description
AddObserver(NSObject, NSString, NSKeyValueObservingOptions, IntPtr) (Inherited from NSObject)
AddObserver(NSObject, String, NSKeyValueObservingOptions, IntPtr) (Inherited from NSObject)
AddObserver(NSString, NSKeyValueObservingOptions, Action<NSObservedChange>)

Registers an object for being observed externally using an arbitrary method.

(Inherited from NSObject)
AddObserver(String, NSKeyValueObservingOptions, Action<NSObservedChange>)

Registers an object for being observed externally using an arbitrary method.

(Inherited from NSObject)
AwakeFromNib() (Inherited from NSObject)
BeginInvokeOnMainThread(Action)

Invokes the specified action asynchronously on the main UI thread.

(Inherited from NSObject)
BeginInvokeOnMainThread(Selector, NSObject)

Invokes asynchronously the specified code on the main UI thread.

(Inherited from NSObject)
BoldSystemFontOfSize(NFloat)

Returns a bold system font of the specified size.

ConformsToProtocol(NativeHandle) (Inherited from NSObject)
Copy() (Inherited from NSObject)
Copy(NSZone)

Performs a copy of the underlying Objective-C object.

DangerousAutorelease()

Calls the 'autorelease' selector on this object.

(Inherited from NSObject)
DangerousRelease()

Calls the 'release' selector on this object.

(Inherited from NSObject)
DangerousRetain()

Calls the 'retain' selector on this object.

(Inherited from NSObject)
DidChange(NSKeyValueChange, NSIndexSet, NSString) (Inherited from NSObject)
DidChange(NSString, NSKeyValueSetMutationKind, NSSet) (Inherited from NSObject)
DidChangeValue(String) (Inherited from NSObject)
Dispose()

Releases the resources used by the NSObject object.

(Inherited from NSObject)
Dispose(Boolean)

Releases the resources used by the NSObject object.

(Inherited from NSObject)
DoesNotRecognizeSelector(Selector) (Inherited from NSObject)
EncodeTo(NSCoder)

Encodes the state of the object using the provided encoder.

Equals(NSObject)

Determines whether the specified NSObject is equal to the current NSObject.

(Inherited from NSObject)
Equals(Object)

Determines whether the specified object is equal to the current UIFont.

FontNamesForFamilyName(String)
FromDescriptor(UIFontDescriptor, NFloat)

Creates a UIFont from the specified font descriptor.

FromName(String, NFloat)

Creates a font with the specified name and size.

GetDictionaryOfValuesFromKeys(NSString[]) (Inherited from NSObject)
GetHashCode()

Generates a hash code for the current instance.

GetMethodForSelector(Selector) (Inherited from NSObject)
GetMonospacedSystemFont(NFloat, NFloat)

Gets the monospaced system font of the specified size and weight.

GetMonospacedSystemFont(NFloat, UIFontWeight)

Gets the monospaced system font of the specified size and weight.

GetNativeHash() (Inherited from NSObject)
GetPreferredFontForTextStyle(NSString, UITraitCollection)

Gets the preferred font for the specified text style and trait collection.

GetPreferredFontForTextStyle(NSString)

Gets the preferred font for the specified text style.

GetPreferredFontForTextStyle(UIFontTextStyle, UITraitCollection)

Gets the UIFont that is preferred by the system for the specified uiFontTextStyle and traitCollection.

GetPreferredFontForTextStyle(UIFontTextStyle)

Gets the preferred font for the specified text style.

Init()

Initializes the object by calling the Objective-C init method.

(Inherited from NSObject)
InitializeHandle(NativeHandle, String) (Inherited from NSObject)
InitializeHandle(NativeHandle) (Inherited from NSObject)
Invoke(Action, Double)

Invokes the specified action after the specified delay.

(Inherited from NSObject)
Invoke(Action, TimeSpan)

Invokes the specified action after the specified delay.

(Inherited from NSObject)
InvokeOnMainThread(Action)

Invokes the specified action synchronously on the main UI thread.

(Inherited from NSObject)
InvokeOnMainThread(Selector, NSObject)

Invokes synchronously the specified code on the main UI thread.

(Inherited from NSObject)
IsEqual(NSObject) (Inherited from NSObject)
IsKindOfClass(Class) (Inherited from NSObject)
IsMemberOfClass(Class) (Inherited from NSObject)
ItalicSystemFontOfSize(NFloat)

Creates an italicized system font of the specified size.

MarkDirty()

Promotes a regular peer object (IsDirectBinding is true) into a toggleref object.

(Inherited from NSObject)
MonospacedDigitSystemFontOfSize(NFloat, NFloat)

Returns the system monospaced font specialized for digits, in the specified size and weight.

MonospacedDigitSystemFontOfSize(NFloat, UIFontWeight)

Gets the monospaced UIFont preferred by the system for displaying digits, of the specified fontSize and weight.

MutableCopy() (Inherited from NSObject)
ObserveValue(NSString, NSObject, NSDictionary, IntPtr) (Inherited from NSObject)
PerformSelector(Selector, NSObject, Double, NSString[]) (Inherited from NSObject)
PerformSelector(Selector, NSObject, Double) (Inherited from NSObject)
PerformSelector(Selector, NSObject, NSObject) (Inherited from NSObject)
PerformSelector(Selector, NSObject) (Inherited from NSObject)
PerformSelector(Selector, NSThread, NSObject, Boolean, NSString[]) (Inherited from NSObject)
PerformSelector(Selector, NSThread, NSObject, Boolean) (Inherited from NSObject)
PerformSelector(Selector) (Inherited from NSObject)
PrepareForInterfaceBuilder() (Inherited from NSObject)
RemoveObserver(NSObject, NSString, IntPtr) (Inherited from NSObject)
RemoveObserver(NSObject, NSString) (Inherited from NSObject)
RemoveObserver(NSObject, String, IntPtr) (Inherited from NSObject)
RemoveObserver(NSObject, String) (Inherited from NSObject)
RespondsToSelector(Selector) (Inherited from NSObject)
SetNilValueForKey(NSString) (Inherited from NSObject)
SetValueForKey(NSObject, NSString) (Inherited from NSObject)
SetValueForKeyPath(NativeHandle, NSString)

Sets the value for the property identified by a given key path to a given value.

(Inherited from NSObject)
SetValueForKeyPath(NSObject, NSString) (Inherited from NSObject)
SetValueForUndefinedKey(NSObject, NSString) (Inherited from NSObject)
SetValuesForKeysWithDictionary(NSDictionary) (Inherited from NSObject)
SystemFontOfSize(NFloat, NFloat)

Returns the default system font in the specified size and weight.

SystemFontOfSize(NFloat, UIFontWeight, UIFontWidth)

Creates a system font of the specified size, weight, and width.

SystemFontOfSize(NFloat, UIFontWeight)

Gets the system font for the specified size and weight.

SystemFontOfSize(NFloat)

Creates a system font of the specified size.

ToString()

Returns a string representation of the value of the current instance.

ValueForKey(NSString) (Inherited from NSObject)
ValueForKeyPath(NSString) (Inherited from NSObject)
ValueForUndefinedKey(NSString) (Inherited from NSObject)
WillChange(NSKeyValueChange, NSIndexSet, NSString) (Inherited from NSObject)
WillChange(NSString, NSKeyValueSetMutationKind, NSSet) (Inherited from NSObject)
WillChangeValue(String) (Inherited from NSObject)
WithSize(NFloat)

Returns a new font based on the current one, with the specified size.

Operators

Name Description
Equality(UIFont, UIFont)

Compares two UIFont objects for value equality.

Inequality(UIFont, UIFont)

Compares two UIFont objects for value inequality.

Extension Methods

Name Description
AccessibilityHitTest(NSObject, CGPoint, UIEvent)
BrowserAccessibilityDeleteTextAtCursor(NSObject, IntPtr)
BrowserAccessibilityInsertTextAtCursor(NSObject, String)
GetAccessibilityCustomRotors(NSObject)

Gets the array of UIAccessibilityCustomRotor objects appropriate for this object.

GetAccessibilityLineEndPositionFromCurrentSelection(NSObject)
GetAccessibilityLineRangeForPosition(NSObject, IntPtr)
GetAccessibilityLineStartPositionFromCurrentSelection(NSObject)
GetAccessibilityNextTextNavigationElement(NSObject)
GetAccessibilityNextTextNavigationElementBlock(NSObject)
GetAccessibilityPreviousTextNavigationElement(NSObject)
GetAccessibilityPreviousTextNavigationElementBlock(NSObject)
GetAccessibilityTextInputResponder(NSObject)
GetAccessibilityTextInputResponderHandler(NSObject)
GetBrowserAccessibilityAttributedValue(NSObject, NSRange)
GetBrowserAccessibilityContainerType(NSObject)
GetBrowserAccessibilityCurrentStatus(NSObject)
GetBrowserAccessibilityHasDomFocus(NSObject)
GetBrowserAccessibilityIsRequired(NSObject)
GetBrowserAccessibilityPressedState(NSObject)
GetBrowserAccessibilityRoleDescription(NSObject)
GetBrowserAccessibilitySelectedTextRange(NSObject)
GetBrowserAccessibilitySortDirection(NSObject)
GetBrowserAccessibilityValue(NSObject, NSRange)
GetDebugDescription(INSObjectProtocol)
GetHandle(INativeObject)
GetNonNullHandle(INativeObject, String)
ProvideImageData(NSObject, IntPtr, UIntPtr, UIntPtr, UIntPtr, UIntPtr, UIntPtr, NSObject)
ProvideImageToMTLTexture(NSObject, IMTLTexture, IMTLCommandBuffer, UIntPtr, UIntPtr, UIntPtr, UIntPtr, NSObject)
SetAccessibilityCustomRotors(NSObject, UIAccessibilityCustomRotor[])

Sets the array of UIAccessibilityCustomRotor objects appropriate for this object.

SetAccessibilityNextTextNavigationElement(NSObject, NSObject)
SetAccessibilityNextTextNavigationElementBlock(NSObject, AXObjectReturnBlock)
SetAccessibilityPreviousTextNavigationElement(NSObject, NSObject)
SetAccessibilityPreviousTextNavigationElementBlock(NSObject, AXObjectReturnBlock)
SetAccessibilityTextInputResponder(NSObject, IUITextInput)
SetAccessibilityTextInputResponderHandler(NSObject, UITextInputReturnHandler)
SetBrowserAccessibilityContainerType(NSObject, BEAccessibilityContainerType)
SetBrowserAccessibilityCurrentStatus(NSObject, String)
SetBrowserAccessibilityHasDomFocus(NSObject, Boolean)
SetBrowserAccessibilityIsRequired(NSObject, Boolean)
SetBrowserAccessibilityPressedState(NSObject, BEAccessibilityPressedState)
SetBrowserAccessibilityRoleDescription(NSObject, String)
SetBrowserAccessibilitySelectedTextRange(NSObject, NSRange)
SetBrowserAccessibilitySortDirection(NSObject, String)
SetSharedObservers(NSObject, NSKeyValueSharedObserversSnapshot)

Applies to

See also