Class

NSFontManager

NSFontManager is the center of activity for the font conversion system. It records the currently selected font, updates the Font panel and Font menu to reflect the selected font, initiates font changes, and converts fonts in response to requests from text-bearing objects. In a more prosaic role, NSFontManager can be queried for the fonts available to the application and for the particular attributes of a font, such as whether it’s condensed or extended.

Overview

You normally set up a font manager and the Font menu using Interface Builder. However, you can also do so programmatically by getting the shared font manager instance and having it create the standard Font menu at runtime:

NSFontManager *fontManager = [NSFontManager sharedFontManager];
NSMenu *fontMenu = [fontManager fontMenu:YES];

You can then add the Font menu to your application’s main menu. After the Font menu is installed, your application automatically gains the functionality of both the Font menu and the Font panel.

Font collections are managed by NSFontManager.

Symbols

Getting the Shared Font Manager

class func shared()

Returns the shared instance of the font manager for the application, creating it if necessary.

Changing the Default Font Conversion Classes

class func setFontManagerFactory(AnyClass?)

Sets the class object used to create the font manager to the given class.

class func setFontPanelFactory(AnyClass?)

Sets the class used to create the Font panel to the given class.

Getting Available Fonts

var availableFonts: [String]

The names of the fonts available in the system (not the NSFont objects themselves).

var availableFontFamilies: [String]

The names of the font families available in the system.

func availableFontNames(with: NSFontTraitMask)

Returns the names of the fonts available in the system whose traits are described exactly by the given font trait mask (not the NSFont objects themselves).

func availableMembers(ofFontFamily: String)

Returns an array with one entry for each available member of a font family.

Setting and Examining the Selected Font

func setSelectedFont(NSFont, isMultiple: Bool)

Records the given font as the currently selected font and updates the Font panel to reflect this.

var selectedFont: NSFont?

The last font recorded.

var isMultiple: Bool

A Boolean value that indicates if the last font selection recorded has multiple fonts.

func sendAction()

A Boolean value that indicates if the font manager’s action message was handled.

func localizedName(forFamily: String, face: String?)

Returns a localized string with the name of the specified font family and face, if one exists.

Sending Action Methods

func addFontTrait(Any?)

This action method causes the receiver to send its action message up the responder chain.

func removeFontTrait(Any?)

This action method causes the receiver to send its action message up the responder chain.

func modifyFont(Any?)

This action method causes the receiver to send its action message up the responder chain.

func modifyFontViaPanel(Any?)

This action method causes the receiver to send its action message up the responder chain.

func orderFrontStylesPanel(Any?)

This action method opens the Font styles panel.

func orderFrontFontPanel(Any?)

This action method opens the Font panel by sending it an orderFront(_:) message, creating the Font panel if necessary.

Converting Fonts Automatically

func convert(NSFont)

Converts the given font according to the object that initiated a font change, typically the Font panel or Font menu.

func changeFont(Any?)

Informs responders of a font change.

Converting Fonts Manually

func convert(NSFont, toFace: String)

Returns a font whose traits are as similar as possible to those of the given font except for the typeface, which is changed to the given typeface.

func convert(NSFont, toFamily: String)

Returns a font whose traits are as similar as possible to those of the given font except for the font family, which is changed to the given family.

func convert(NSFont, toHaveTrait: NSFontTraitMask)

Returns a font whose traits are the same as those of the given font, except that the traits are changed to include the single specified trait.

func convert(NSFont, toNotHaveTrait: NSFontTraitMask)

Returns an NSFont object with the same traits as the given font, except for the traits in the given font trait mask, which are removed.

func convert(NSFont, toSize: CGFloat)

Returns an NSFont object whose traits are the same as those of the given font, except for the size, which is changed to the given size.

func convertWeight(Bool, of: NSFont)

Returns an NSFont object whose weight is greater or lesser than that of the given font, if possible.

var currentFontAction: NSFontAction

The current font conversion action.

func convertFontTraits(NSFontTraitMask)

Converts font traits to a new traits mask value.

Getting a Particular Font

func font(withFamily: String, traits: NSFontTraitMask, weight: Int, size: CGFloat)

Attempts to load a font with the specified characteristics.

Examining Fonts

func traits(of: NSFont)

Returns the traits of the given font.

func fontNamed(String, hasTraits: NSFontTraitMask)

Indicates whether the given font has all the specified traits.

func weight(of: NSFont)

Returns a rough numeric measure the weight of the given font.

Managing the Font Panel and Font Menu

var isEnabled: Bool

A Boolean value that indicates if the font conversion system’s user interface items (the Font panel and Font menu items) are enabled.

func fontManager(Any, willIncludeFont: String)

Requests permission from the Font panel delegate to display the given font name in the Font panel.

Deprecated
func fontPanel(Bool)

Returns the application’s shared Font panel object, optionally creating it if necessary.

func setFontMenu(NSMenu)

Records the given menu as the application’s Font menu.

func fontMenu(Bool)

Returns the menu that’s hooked up to the font conversion system, optionally creating it if necessary.

Accessing the Delegate

var delegate: AnyObject?

The font manager’s delegate.

Deprecated

Accessing the Action Property

var action: Selector

The action sent to the first responder when the user selects a new font from the Font panel or chooses a command from the Font menu.

var target: AnyObject?

The target for the action associated with the sendAction() property.

Setting Attributes

func setSelectedAttributes([String : Any], isMultiple: Bool)

Informs the paragraph and character formatting panels when text in a selection has changed attributes.

func convertAttributes([String : Any] = [:])

Converts attributes in response to an object initiating an attribute change, typically the Font panel or Font menu.

Working with Font Descriptors

func availableFontNames(matching: NSFontDescriptor)

Returns the names of the fonts that match the attributes in the given font descriptor.

Deprecated
var collectionNames: [Any]

The names of the currently loaded font collections.

Deprecated
func fontDescriptors(inCollection: String)

Returns an array of the font descriptors in the collection specified by the given collection name.

Deprecated
func addCollection(String, options: NSFontCollectionOptions = [])

Adds a specified font collection to the font manager with a given set of options.

Deprecated
func removeCollection(String)

Removes the specified font collection.

Deprecated
func addFontDescriptors([Any], toCollection: String)

Adds an array of font descriptors to the specified font collection.

Deprecated
func removeFontDescriptor(NSFontDescriptor, fromCollection: String)

Removes the specified font descriptor from the specified collection.

Deprecated

Constants

Font Collection Mask

This constant specifies options accepted by addCollection(_:options:).

NSFontTraitMask

Mask of traits assigned to a font, assigned using the values in Font traits.

Font traits

Font traits defined and supported by NSFontManager.

NSFontAction

These constants specify what action modifyFont(_:) will take.

Relationships

Inherits From

Conforms To