On Jun 15, 2009, at 6:31 PM, Stuart Malin wrote:

I am replying to my own posting with updated info, for the archives...

I would like for my app to enable the user to set a user default for a
Font to be used in a certain part of the application. I have a
preferences panel, and within that am able to open the NSFontPanel.
However, I can't figure out how to capture the setting that is made:
must I actually have some text (say in a Text View) that is edited?

I had been invoking the NSFontManager from outside the App delegate, which was the final place I managed to intercept the changeFont: call (as it rode up the Responder chain). Alas, I had tried setting the delegate for the Font Manager to call the object that needed the result of the Font Panel settings made by the user. I then had my "aha" when I found the -setAction and -setTarget methods; setting these solved that problem.

Also, I had been separately invoking the NSFontManager and the NSFontPanel. Rather than open an instance of the Font Panel directly, I have the Font Manger do it: [fontManager orderFrontFontPanel:nil];

With these changes, my app can open the Font Panel from some controller other than the App delegate, and capture the settings made by the user.

For completeness, the invoking method code is (of course this has details specific to my app):

        NSFontManager *fontManager = [NSFontManager sharedFontManager];
        [fontManager setTarget: self];
        [fontManager setAction:@selector(changeFontForInfoText:)];
        [fontManager setSelectedFont:mInfoTextFont isMultiple: NO];
        [fontManager orderFrontFontPanel:nil];

The method that is informed of the font changes made in the Font Panel is simple:

- (void) changeFontForInfoText:(id)sender
{
        NSFontManager *fontManager = [NSFontManager sharedFontManager];
        NSFont *font = [fontManager convertFont:[fontManager selectedFont]];
        // do whatever else needs to be done with the new font
}

Also, is it possible to save an NSFont as an object in user
preferences? Or must I save off the parts of the font specification
separately: font name, size, attributes.

I tried various approaches, including storing and loading the attributes dictionary of the NSFontDescriptor for the font. In the end, I just save two fields, the font name and the point size. While this doesn't allow for saving and restoring detailed tweaks to the font, my app doesn't need such, and the two field approach requires less manipulation of intermediary objects, so I opted for that approach.

~~~

hope this info is useful to someone someday....



_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to