This snippet contains an overview of the method by which font attributes are handled. There are many possible attributes that can be set, and I’ll cover them, and their methods, in another snippet.
Contents
Context: Text handling
Cocoa gives you a font menu, font management functions, and All That Jazz for free. That’s the easy way for a user to set font attributes. (For a much more detailed Overview see the Text Handling Overview)
If you want to allow users to set a default font, use a font pallette or other ways for the user to style text, this will need to be done in code.
Underlying Principle
You create a font object with the attributes of the font currently in the text field, manipulate that object, and apply it back to the text field. At least, the examples in _David Chisnall: Cocoa Programming Fundamentals_ work on this basis. It’s one of the things that leave me wondering whether this is the only way *to* set font attributes, the best way, or what.
On a further tentacle, this appears to be the same principle that works on creating user-defined styles and default styles: instead of creating a temporary object you manipulate, apply, and release immediately, you store it (I have no idea in which form) so you can _then_ apply it to text.
Prerequisites
Access to an/the? instance of NSFontManager (Apple Developer Library Link), which – if I understand it correctly – is a singleton, so there should be only one.
In _David Chisnall: Cocoa Programming Fundamentals_, this is solved through a class initialisation method, but don’t ask me to explain the exact reasoning behind it or under which circumstances this is a good or bad idea.
NSFontManager *fm;
@implementation FontController
+ (void) initialize
{
fm = [NSFontManager sharedFontManager];
}[/obc]
(I'm recording it because it's important, but I make no claims to actually understanding the reasoning behind this.)
<h3>Setting Font Attribute</h3>
First, get your font attribute. In this example I'll stick with the font family.
<h4>Interface Builder</h4>
To get all available font families (allegedly), bind the contentKey of an <code>NSComboBox</code> to the 'availableFontFamilies' key of the FontManager. (I say allegedly because when I tried this in Xcode 2.5 I got a menu populated with a selection of my fonts, out of order. I don't have the opportunity to test this in Xcode 3.5 right now.)
<h4>IBAction</h4>
[objc]- (IBAction) takeFontFamilyFrom: (id)sender
{
NSFont = *font [text font];
font = [fm convertFont: font
toFamily: [sender stringValue]]; //sender = NSComboBox
[text setFont: font];
}
(_David Chisnall: Cocoa Programming Fundamentals_)
It becomes obvious here that the convertFont: toFamily: method needs a font object to work on.
Caveats
I am not certain about the principle, but I’m writing it down anyway lest I lose sight of it. Create font object-manipulate it – apply to text field makes sense to me.
Linkage
(to be implemented)
Antidotes
- Font Menu
Entanglements
- Font Attributes
- User-defined Styles
Areas
- Text Processing