| Inherits from | |
| Conforms to | |
| Framework | /System/Library/Frameworks/AppKit.framework |
| Availability | Available in Mac OS X v10.0 and later.
|
| Companion guide | |
| Declared in | NSFontManager.h |
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.
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. Once the Font menu is installed, your application automatically gains the functionality of both the Font menu and the Font panel.
As of Mac OS X version 10.3, font collections are managed by NSFontManager.
– availableFonts
– availableFontFamilies
– availableFontNamesWithTraits:
– availableMembersOfFontFamily:
– setSelectedFont:isMultiple:
– selectedFont
– isMultiple
– sendAction
– localizedNameForFamily:face:
– addFontTrait:
– removeFontTrait:
– modifyFont:
– modifyFontViaPanel:
– orderFrontStylesPanel:
– orderFrontFontPanel:
– convertFont:
– changeFont: delegate method
– convertFont:toFace:
– convertFont:toFamily:
– convertFont:toHaveTrait:
– convertFont:toNotHaveTrait:
– convertFont:toSize:
– convertWeight:ofFont:
– currentFontAction
– convertFontTraits:
– setEnabled:
– isEnabled
– fontManager:willIncludeFont: delegate method
– fontPanel:
– setFontMenu:
– fontMenu:
– availableFontNamesMatchingFontDescriptor:
– collectionNames
– fontDescriptorsInCollection:
– addCollection:options:
– removeCollection:
– addFontDescriptors:toCollection:
– removeFontDescriptor:fromCollection:
Sets the class object used to create the font manager to the given class.
+ (void)setFontManagerFactory:(Class)aClass
The new font manager factory class, which should be a subclass of NSFontManager.
When the NSFontManager class object receives a sharedFontManager message, it creates an instance of aClass, if no instance already exists. Your font manager class should implement init as its designated initializer. The default font manager factory is NSFontManager.
This method must be invoked before your application’s main nib file is loaded, such as in the application delegate’s applicationWillFinishLaunching: method.
NSFontManager.h
Sets the class used to create the Font panel to the given class.
+ (void)setFontPanelFactory:(Class)factoryId
The new font panel factory class, which should be a subclass of NSFontPanel.
Invoke this method before accessing the Font panel in any way, such as in the application delegate’s applicationWillFinishLaunching: method.
NSFontManager.h
Returns the shared instance of the font manager for the application, creating it if necessary.
+ (NSFontManager *)sharedFontManager
The shared font manager.
NSFontManager.hReturns 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.
- (SEL)action
The selector for the action.
The default action is changeFont:.
NSFontManager.hAdds a specified font collection to the font manager with a given set of options.
- (BOOL)addCollection:(NSString *)collectionName options:(NSInteger)collectionOptions
The collection to add.
The option described in “Font Collection Mask”. This option is not yet implemented.
YES if the font collection was successfully added; otherwise, NO.
NSFontManager.hAdds an array of font descriptors to the specified font collection.
- (void)addFontDescriptors:(NSArray *)descriptors toCollection:(NSString *)collectionName
The font descriptors to add.
The font collection to which descriptors are added.
NSFontManager.hThis action method causes the receiver to send its action message up the responder chain.
- (void)addFontTrait:(id)sender
The control that sent the message.
By default, the action message is changeFont:.
When a responder replies by providing a font to convert in a convertFont: message, the receiver converts the font by adding the trait specified by sender. This trait is determined by sending a tag message to sender and interpreting it as a font trait mask for a convertFont:toHaveTrait: message.
NSFontManager.hReturns the names of the font families available in the system.
- (NSArray *)availableFontFamilies
The names of the available font families.
These fonts are in various system font directories.
NSFontManager.hReturns the names of the fonts that match the attributes in the given font descriptor.
- (NSArray *)availableFontNamesMatchingFontDescriptor:(NSFontDescriptor *)descriptor
The font descriptor whose attributes are matched.
The names of the matching fonts.
NSFontManager.hReturns 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).
- (NSArray *)availableFontNamesWithTraits:(NSFontTraitMask)fontTraitMask
The font traits for which to return font names. You specify the desired traits by combining the font trait mask values described in “Constants” using the C bitwise OR operator.
The names of the corresponding fonts.
These fonts are in various system font directories.
If fontTraitMask is 0, this method returns all fonts that are neither italic nor bold. This result is the same one you’d get if fontTraitMask were NSUnitalicFontMask | NSUnboldFontMask.
NSFontManager.hReturns the names of the fonts available in the system (not the NSFont objects themselves).
- (NSArray *)availableFonts
The names of the available fonts.
These fonts are in various system font directories.
NSFontManager.hReturns an array with one entry for each available member of a font family.
- (NSArray *)availableMembersOfFontFamily:(NSString *)family
The name of a font family, like one that availableFontFamilies returns.
The available members of family. See the following discussion for a specific description.
Each entry of the returned NSArray is another NSArray with four members, as follows:
0. The PostScript font name, as an NSString object.
1. The part of the font name used in the font panel that’s not the font name, as an NSString object. This value is not localized—for example, "Roman", "Italic", or "Bold".
2. The font’s weight, as an NSNumber.
3. The font’s traits, as an NSNumber.
The members of the family are arranged in the font panel order (narrowest to widest, lightest to boldest, plain to italic).
For example, if you call availableMembersOfFontFamily:@"Times", it might return an array like this:
(("Times-Roman", "Roman", 5, 4), |
("Times-Italic", "Italic", 6, 5), |
("Times-Bold", "Bold", 9, 2), |
("Times-BoldItalic", "Bold Italic", 9, 3) |
) |
NSFontManager.hReturns the names of the currently loaded font collections.
- (NSArray *)collectionNames
The names of the current font collections.
NSFontManager.hConverts attributes in response to an object initiating an attribute change, typically the Font panel or Font menu.
- (NSDictionary *)convertAttributes:(NSDictionary *)attributes
The current attributes.
The converted attributes, or attributes itself if the conversion isn’t possible.
Attributes unused by the sender should not be changed or removed.
This method is usually invoked on the sender of changeAttributes:. See NSTextView for more information.
NSFontManager.hConverts the given font according to the object that initiated a font change, typically the Font panel or Font menu.
- (NSFont *)convertFont:(NSFont *)aFont
The font to convert.
The converted font, or aFont itself if the conversion isn’t possible.
This method is invoked in response to an action message such as addFontTrait: or modifyFontViaPanel:. These initiating methods cause the font manager to query the sender for the action to take and the traits to change. See “Converting Fonts Manually” for more information.
– convertFont:toFace:– convertFont:toFamily:– convertFont:toHaveTrait:– convertFont:toNotHaveTrait:– convertFont:toSize:– convertWeight:ofFont:NSFontManager.hReturns 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.
- (NSFont *)convertFont:(NSFont *)aFont toFace:(NSString *)typeface
The font whose traits are matched.
The new typeface; a fully specified family-face name, such as Helvetica-BoldOblique or Times-Roman.
A font with matching traits and the given typeface, or aFont if it can’t be converted.
This method attempts to match the weight and posture of aFont as closely as possible. Italic is mapped to Oblique, for example. Weights are mapped based on an approximate numeric scale of 0 to 15.
– convertFont:toFamily:– convertFont:toHaveTrait:– convertFont:toNotHaveTrait:– convertFont:toSize:– convertWeight:ofFont:– convertFont:NSFontManager.hReturns 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.
- (NSFont *)convertFont:(NSFont *)aFont toFamily:(NSString *)family
The font whose traits are matched.
The new font famliy; a generic font name, such as Helvetica or Times.
A font with matching traits and the given family, or aFont if it can’t be converted.
This method attempts to match the weight and posture of aFont as closely as possible. Italic is mapped to Oblique, for example. Weights are mapped based on an approximate numeric scale of 0 to 15.
– convertFont:toFace:– convertFont:toHaveTrait:– convertFont:toNotHaveTrait:– convertFont:toSize:– convertWeight:ofFont:– convertFont:NSFontManager.hReturns 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.
- (NSFont *)convertFont:(NSFont *)aFont toHaveTrait:(NSFontTraitMask)fontTrait
The font whose traits are matched.
The new trait; may be any one of the traits described in “Constants.” Using NSUnboldFontMask or NSUnitalicFontMask removes the bold or italic trait, respectively.
A font with matching traits including the given trait, or aFont if it can’t be converted.
Using NSUnboldFontMask or NSUnitalicFontMask removes the bold or italic trait, respectively.
– convertFont:toNotHaveTrait:– convertFont:toFace:– convertFont:toFamily:– convertFont:toSize:– convertWeight:ofFont:– convertFont:NSFontManager.hReturns an NSFont object with the same traits as the given font, except for the traits in the given font trait mask, which are removed.
- (NSFont *)convertFont:(NSFont *)aFont toNotHaveTrait:(NSFontTraitMask)fontTraitMask
The font whose traits are matched.
The mask for the traits to remove, created using the C bitwise OR operator to combine the traits described in “Constants.” Using NSUnboldFontMask or NSUnitalicFontMask removes the bold or italic trait, respectively.
A font with matching traits minus the given traits, or aFont if it can’t be converted.
– convertFont:toHaveTrait:– convertFont:toFace:– convertFont:toFamily:– convertFont:toSize:– convertWeight:ofFont:– convertFont:NSFontManager.hReturns 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.
- (NSFont *)convertFont:(NSFont *)aFont toSize:(CGFloat)size
The font whose traits are matched.
The new font size.
A font with matching traits except in the new size, or aFont if it can’t be converted.
– convertFont:toFace:– convertFont:toFamily:– convertFont:toHaveTrait:– convertFont:toNotHaveTrait:– convertWeight:ofFont:– convertFont:NSFontManager.hConverts font traits to a new traits mask value.
- (NSFontTraitMask)convertFontTraits:(NSFontTraitMask)traits
The current font traits.
The new traits mask value to be used by convertFont:.
This method is intended to be invoked to query the font traits while the action message (usually changeFont:) is being invoked when the current font action is either NSAddTraitFontAction or NSRemoveTraitFontAction.
NSFontManager.hReturns an NSFont object whose weight is greater or lesser than that of the given font, if possible.
- (NSFont *)convertWeight:(BOOL)increaseFlag ofFont:(NSFont *)aFont
If YES, a heavier font is returned; if it’s NO, a lighter font is returned.
The font whose weight is increased or decreased.
A font with matching traits except for the new weight, or aFont if it can’t be converted.
Weights are graded along the following scale. The list on the left gives Apple’s terminology, and the list on the right gives the ISO equivalents. Names on the same line are treated as identical:
Apple Terminology |
ISO Equivalent |
|---|---|
1. ultralight |
|
2. thin |
W1. ultralight |
3. light, extralight |
W2. extralight |
4. book |
W3. light |
5. regular, plain, display, roman |
W4. semilight |
6. medium |
W5. medium |
7. demi, demibold |
|
8. semi, semibold |
W6. semibold |
9. bold |
W7. bold |
10. extra, extrabold |
W8. extrabold |
11. heavy, heavyface |
|
12. black, super |
W9. ultrabold |
13. ultra, ultrablack, fat |
|
14. extrablack, obese, nord |
The NSFontManager implementation of this method refuses to convert a font’s weight if it can’t maintain all other traits, such as italic and condensed. You might wish to override this method to allow a looser interpretation of weight conversion.
– convertFont:toFace:– convertFont:toFamily:– convertFont:toHaveTrait:– convertFont:toNotHaveTrait:– convertFont:toSize:– convertFont:NSFontManager.hReturns the current font conversion action.
- (NSFontAction)currentFontAction
The current font action used by the convertFont: method.
This method is intended to be invoked to query the font conversion action while the action message (usually changeFont:) is being invoked.
NSFontManager.hReturns the receiver’s delegate.
- (id)delegate
The receiver’s delegate.
NSFontManager.hReturns an array of the font descriptors in the collection specified by the given collection name.
- (NSArray *)fontDescriptorsInCollection:(NSString *)collectionName
The font collection for which to return descriptors.
The font descriptors.
NSFontManager.hReturns the menu that’s hooked up to the font conversion system, optionally creating it if necessary.
- (NSMenu *)fontMenu:(BOOL)createFlag
The font conversion system menu.
NSFontManager.hIndicates whether the given font has all the specified traits.
- (BOOL)fontNamed:(NSString *)typeface hasTraits:(NSFontTraitMask)fontTraitMask
The name of the font.
The font traits to test, specified by combining the font trait mask values described in “Constants” using the C bitwise OR operator.
YES if the font named typeface has all the traits specified in fontTraitMask; NO if it doesn’t.
Using NSUnboldFontMask returns YES if the font is not bold, NO otherwise. Using NSUnitalicFontMask returns YES if the font is not italic, NO otherwise.
NSFontManager.h
Returns the application’s shared Font panel object, optionally creating it if necessary.
- (NSFontPanel *)fontPanel:(BOOL)createFlag
The application’s shared Font panel object.
+ sharedFontPanel (NSFontPanel)+ sharedFontPanelExists (NSFontPanel)+ setFontPanelFactory:NSFontManager.hAttempts to load a font with the specified characteristics.
- (NSFont *)fontWithFamily:(NSString *)family traits:(NSFontTraitMask)fontTraitMask weight:(NSInteger)weight size:(CGFloat)size
The generic name of the desired font, such as Times or Helvetica.
The font traits, specified by combining the font trait mask values described in “Constants” using the C bitwise OR operator. Using NSUnboldFontMask or NSUnitalicFontMask loads a font that doesn’t have either the bold or italic trait, respectively.
A hint for the weight desired, on a scale of 0 to 15: a value of 5 indicates a normal or book weight, and 9 or more a bold or heavier weight. The weight is ignored if fontTraitMask includes NSBoldFontMask.
The point size of the desired font.
A font with the specified characteristics if successful, or nil if not.
NSFontManager.hIndicates whether the font conversion system’s user interface items (the Font panel and Font menu items) are enabled.
- (BOOL)isEnabled
YES if the font conversion system’s user interface items (the Font panel and Font menu items) are enabled; NO if they’re not.
– isEnabled (NSFontPanel)– isEnabled (NSMenuItem)– setEnabled:NSFontManager.h
Indicates whether the last font selection recorded has multiple fonts.
- (BOOL)isMultiple
YES if the last font selection recorded has multiple fonts; NO if it’s a single font.
NSFontManager.hReturns a localized string with the name of the specified font family and face, if one exists.
- (NSString *)localizedNameForFamily:(NSString *)family face:(NSString *)face
The font family, for example, @"Times".
The font face, for example, @"Roman".
A localized string with the name of the specified font family and face, or, if face is nil, the font family only.
The user’s locale is determined from the user’s NSLanguages default setting. The method also loads the localized strings for the font, if they aren’t already loaded.
NSFontManager.hThis action method causes the receiver to send its action message up the responder chain.
- (void)modifyFont:(id)sender
The control that sent the message.
By default, the action message is changeFont:.
When a responder replies by providing a font to convert in a convertFont: message, the receiver converts the font in the manner specified by sender. The conversion is determined by sending a tag message to sender and invoking a corresponding method:
Sender’s Tag |
Method Used |
|---|---|
None; the font is returned unchanged. |
|
The Font panel’s |
|
NSFontManager.hThis action method causes the receiver to send its action message up the responder chain.
- (void)modifyFontViaPanel:(id)sender
The control that sent the message.
By default, the action message is changeFont:.
When a responder replies by providing a font to convert in a convertFont: message, the receiver converts the font by sending a panelConvertFont: message to the Font panel. The panel in turn may send convertFont:toFamily:, convertFont:toHaveTrait:, and other specific conversion methods to make its change.
NSFontManager.h
This action method opens the Font panel by sending it an orderFront: message, creating the Font panel if necessary.
- (void)orderFrontFontPanel:(id)sender
The control that sent the message.
NSFontManager.hThis action method opens the Font styles panel.
- (void)orderFrontStylesPanel:(id)sender
The control that sent the message.
NSFontManager.hRemoves the specified font collection.
- (BOOL)removeCollection:(NSString *)collectionName
The collection to remove.
YES if the font collection was successfully removed; otherwise, NO.
NSFontManager.hRemoves the specified font descriptor from the specified collection.
- (void)removeFontDescriptor:(NSFontDescriptor *)descriptor fromCollection:(NSString *)collection
The font descriptor to remove.
The font collection from which to remove the descriptor.
NSFontManager.hThis action method causes the receiver to send its action message up the responder chain.
- (void)removeFontTrait:(id)sender
The control that sent the message.
By default, the action message is changeFont:.
When a responder replies by providing a font to convert in a convertFont: message, the receiver converts the font by removing the trait specified by sender. This trait is determined by sending a tag message to sender and interpreting it as a font trait mask for a convertFont:toNotHaveTrait: message.
NSFontManager.h
Returns the last font recorded.
- (NSFont *)selectedFont
The last font recorded with a setSelectedFont:isMultiple: message
While fonts are being converted in response to a convertFont: message, you can determine the font selected in the Font panel like this:
NSFontManager *fontManager = [NSFontManager sharedFontManager]; |
panelFont = [fontManager convertFont:[fontManager selectedFont]]; |
NSFontManager.h
Sends the receiver’s action message up the responder chain, initiating a font change for whatever conversion and trait to change were last requested.
- (BOOL)sendAction
YES if some object handled the changeFont: message; NO if the message went unheard.
By default, the receiver’s action message is changeFont:.
This method is used internally by the font conversion system. You should never need to invoke it directly. Instead, use the action methods such as addFontTrait: or modifyFontViaPanel:.
NSFontManager.h
Sets the action that’s sent to the first responder, when the user selects a new font from the Font panel or chooses a command from the Font menu, to the given selector.
- (void)setAction:(SEL)aSelector
The selector to set.
The default action is changeFont:. You should rarely need to change this setting.
NSFontManager.h
Sets the receiver’s delegate to the given object.
- (void)setDelegate:(id)anObject
The new delegate.
NSFontManager.h
Controls whether the font conversion system’s user interface items (the Font panel and Font menu items) are enabled.
- (void)setEnabled:(BOOL)flag
– setEnabled: (NSFontPanel)– isEnabledNSFontManager.hRecords the given menu as the application’s Font menu.
- (void)setFontMenu:(NSMenu *)aMenu
The new Font menu.
NSFontManager.hInforms the paragraph and character formatting panels when text in a selection has changed attributes.
- (void)setSelectedAttributes:(NSDictionary *)attributes isMultiple:(BOOL)flag
The new attributes.
If YES, informs the panel that multiple fonts or attributes are enclosed within the selection.
This method is used primarily by NSTextView.
NSFontManager.h