NSFontPanel in a dialog

Hi, ALL,

I apologize in advance if my question is simple. I'm new to Cocoa development and am trying to learn as much as possible.


My current task is:


My program has a dialog which looks like a property sheet dialog on Windows (ref. https://msdn.microsoft.com/en-us/library/windows/desktop/bb774538(v=vs.85).aspx).

One of the pages/sheets in this dialog should be a font selection, where the user can select font and hit "Apply"/"OK".


I was thinking to implement it using NSFontManager/NSFontPanel, but it looks like I will not be able to, since NSFontPanel is a top-level NSWindow and it will not be possible.


So my questions would be:

1. Am I missing something and it is actually possible to make NSFontPanel a child window?

2. If not - is there another class I can use to make it part of the properties dialog and allow the user to change/select the font?


Thank you in advance for any pointers.

NSFontPanel is old school. I think the time has come for them to modernize it a bit. I don't care for the "floating" behavior of it. With multiple windows open you may forget which window owns the font panel as you change fonts.


You may be able to attach the font panel as a sheet, but I think you'd have to add a "Done" button to dismiss the sheet in the font panels accessory view. Not sure if that would work well (didn't try it).


You could write your own little font chooser and load fonts (retrieved from NSFontManager) in an NSBrowser or something. Not sure if it'd be worth the effort though.

Hi, Macho Man,

How do you do attaching? Can you elaborate a little?


Like I said I'm a newbie in Cocoa programming. I saw that the class existed and thought that I can skip writing my own one.

It looks like I was wrong though.


So there is nothing in the Cocoa API that I can use in the property sheet-like dialog (don't know if something is available under Cocoa and if it is - what its name)?


Thank you.

NSFontPanel is the standard control the system provides for choosing fonts. It typically "just works" with things in Cocoa. I don't know anything about Windows so I won't be able to help you with achieving similar behavior to a control on Windows.


I don't like NSFontpanel, how it floats around and it may not be obvious which window it is connected to. That's just my personal opinion though.


You *could* attach the font panel as a sheet, since it's just a window:


https://developer.apple.com/documentation/appkit/nswindow/1419653-beginsheet?language=objc


But the more I think about it, the more I think that that is a bad idea because NSFontPanel is a singleton. And it is tied to other things in Cocoa (like NSTextView) automatically and it wasn't really designed to be used as a sheet. So that might bite you.


You could settle on just using NSFontPanel as is and just take the easy way out. Or you could create a NSWindowController subclass, get a list of fonts from NSFontManager and load them into an NSBrowser or something if you want and present *that* window as a sheet. But if you're new to Cocoa, it may not be something you want to spend time working on at this stage.

You can start here:


https://developer.apple.com/library/content/documentation/TextFonts/Conceptual/CocoaTextArchitecture/FontHandling/FontHandling.html


Note that the font panel doesn't "belong" to a window. When a font is chosen, it sends a "changeFont:" action message up the responder chain until something handles it. That means the choice applies to the window that currently has "first responder" status, which is (roughly) the frontmost window.

Hi, Quincey,

Yes, unfortunately NSFontPanel is like a top-level window and not like a panel, even though it is named "...Panel".


I did see the page you referenced. I went through it couple of times.

Let me give you my scenario so you can understand what I need and maybe you can suggest something.


I am writing an application in which the user can have multiple objects painted on the frame. Each object contains different set of properties.

Couple of objects can have a different fonts representation, but the fonts are not immediately visible. You need to switch to a different view representation to

see that object with the a text/font.


It is kind of like a design view and the actual presentation view. In the design view you select different options/properties for an object and then when you switch to the actual view you will see an actual data written with {possibly} different fonts.


Now my thought was that I will be able to just create a properties dialog where on of the pages will be a font selector where I can choose a font and store it in that specific object.

Unfortunately it looks like I will have to write the font selector GUI myself and then put in the logic of populating the fonts and selecting the appropriate one.


As you can see, my scenario is a little different from the usual NSFontPanel/NSFontManager one.

Maybe you have a suggestion on how to best handle possible font changing for the object?


TIA!


P.S.: IIUC, I can use functions from here:https://developer.apple.com/documentation/coretext/core_text_functions to populate font names encoding and sizes. Or there are alternatives?

Can't you llow font selection through a choixe in the Edit Menu ? You would activate / deactivate items depending on which window is in the front ?


I did so to let user select the color of the font.

Maybe I misunderstand the problem. Yes, you can't make the font panel/window one of the "pages" of your properties inspector, but you can have one of the pages show what font has been chosen (and perhaps other details), and still allow the font to be re-chosen by selecting a font in the separate font panel. It's not exactly what you were looking for, but it's how the font panel works in macOS. (The system color panel works similarly, too.)


If you want the choice of the font to be integrated into one of your property pages, then you will have to implement the whole thing yourself. I guess you can use CoreText for this, but it would probably be easier to start by trying to query NSFontManager for the information you need.


I suggest you look at the UI in Pages. Its Format panel has controls for choosing fonts, styles and sizes of selected text directly, or you can use Show Font Panel (Command-T) to bring up the font panel and choose fonts from there. Not that the font panel doesn't have to be the frontmost window in order to choose from it. All it requires is that your frontmost window has something in the responder chain that responds the the "changeFont:" action method.


Either approach is fine (and so is having both, like Pages). The only real difference is that the font panel gives you all the typographic controls (collections, glyph variants, OpenType, etc) for free, while putting the UI in your own window is more work for you.

Hi, Claude31,

It is an idea, yes.

However, the I'm doing a cross-platform work and the interface is already chosen.


Thank you.

Hi, Quincey,

You game a good idea - have a panel show the currently selected font and have a button on that panel with the text "Change Font", which will bring the NSFontPanel. Just because I don't want to re-invent the wheel and it is easier for me as a newcomer.


I could try to do it as an excersize later, but that about it.


It would be nice if Apple be more careful of how they name stuff ;-), but I guess it is too late in the game.


Thank you for the suggestion.

I will now try to code such a solution and let you guys know if that worked.


Thank you.


P.S.: I will mark you reply "Solved" after I implement it.

NSFontPanel in a dialog
 
 
Q