Thursday, November 27, 2008

NSFontManager

Here is a minimal demonstration of an application that uses the FontPanel.
The application window contains a Custom View object (class set to MyView). The main menu contains a Font Menu Item (NSMenuItem), and there is a object called Font Manager (NSFontManager). The only connection that is needed is to make the Font Manager a target of the Show Fonts Menu Item, such that it receives the action "orderFrontFontPanel."

Now, when the User clicks on Font => Show Fonts, the Font Manager handles the interaction, and if the font changes, it calls "changeFont_" which is sent to the First Responder. To deal with a request to change the font, all we need is something like this:

class MyView(NSView):

def initWithFrame_(self, frame):
self = super(MyView, self).initWithFrame_(frame)
self.font = NSFont.fontWithName_size_(
'Helvetica', NSNumber.numberWithInt_(14))
return self

def acceptsFirstResponder(self):
return True

def changeFont_(self,sender):
print 'changeFont_', sender
font = sender.convertFont_(self.font)
print font.description()
self.font = font
self.setNeedsDisplay_(True)

def drawRect_(self, rect):
NSColor.whiteColor().set()
NSBezierPath.fillRect_(self.bounds())
D = { NSFontAttributeName: self.font }
g = 'just a purple string'
c = NSColor.purpleColor()
D[NSForegroundColorAttributeName] = c
s = NSString.stringWithString_(g)
s.drawAtPoint_withAttributes_(NSPoint(20,20), D)


I think what it does is so obvious that we don't need a screenshot. Of course, we could make things a lot more complicated. At the moment, none of the other items in the Font Menu is active.

No comments: