So it appears changing the font and size of a text view is rather complicated. Could someone do a “for dummies” style write-up on how to do this? How to make the NSFont method, how to set up the sub-classes, etc… I’m sure many people would appreciate it. Thanks.
Well, that’s not necessarily true. If you’re looking to create custom controls, such as buttons or menu’s that directly control the text in a text view, then yes, it can get a bit more complicated. If you just want basic font panel abilities, then adding font panel support is REALLY easy. In IB, go to the “Cocoa-Menus” pane in the object palettes. Drag a “Font” menu to your application main menu. If you’re OK with having the font menu in your menu bar, you’re done. If that’s not enough for you, check out the instances pane of the MainMenu.nib window. You’ll notice a new instance in there labelled “Font Manager”. (Once you add the font menu the the nib, if you don’t want the font menu in the app you can delete it and the reference to the font manager will still be there.) You can now connect a button or menu item in your interface to the “orderFrontFontPanel:” method of the Font Manager to pop up the font panel at any time… you don’t have to use just the font menu. Simply control-drag from the control to the Font Manager instance, select the method, then click connect. Now when you click the control, the font panel pops up. Whatever the selected text-bearing object is will receive the changeFont message sent by the font panel when you make changes to the font.
If you want to create custom controls, such as menus or buttons which programmatically change the font of an object, you may be able to do some basic stuff in AS, but you might also need to get into some custom obj-c to handle this, depending on your specific task. Let us know what exactly you’re trying to do and if the above doesn’t work for you.
Ah, I should have been more clear. I simply want to set the size and font of a text view when the app opens. I don’t want the user to be able to change it, but the default size is too small, so I’d like to make it a bit bigger in IB or in the awakefromnib if that’s possible. Does this make sense? I just need a way to change the size and font of a text view to Lucinda Grande 24. Thanks.
The following AS code sets the font to lucida grand 24. This will not set the other settings of the text view that control what kind of text can go into the view and whether the user can change the font (many of the text view settings in IB don’t actualy work for some reason, so I’ve added a custom objc subclass below that handles setting both the font and the rest of the settings you might need.)
on awake from nib theObject
if name of theObject is "TextView" then
set myNSFont to call method "fontWithName:size:" of class "NSFont" with parameters {"Lucida Grande", 24}
call method "setFont:" of theObject with parameter myNSFont
end if
end awake from nib
The following subclass of NSTextView will override most of what you need to set your text view to be one font only and to not be editable by the user via the font panel or by cut & paste. If you’re not familiar with setting up custom subclasses, check out my tutorial here.
Yes, ‘TextView’ refers to the name of the text view, so change that to whatever you’ve got your text view named. Of course, be sure to connect it to the handler in IB, too.
As far as the obj-c code goes… once you’ve created your subclass, you must make sure that the textview is set as being of that class. Select the text view (not the enclosing scroll view) and then go to the “Custom Class” pane in IB’s inspector window. Click on your subclass and rebuild. Your text view should now execute the code.
Note that I’m not sure that you can have your text view execute both the AS handler and obj-c awakeFromNib method. If you HAVE TO keep the text view attached to the AS handler, then you’ll want to create a custom method that you can call from your AS handler instead of putting it all in the obj-c AFN method.