setting/keeping font of text view

Howdy all,

I know this question has been asked many times before (As I’ve seen by my searching) but I still cannot find a resolution.

Basically, I have my text view, which will contain the guitar tabs. Now then, the font in this field must be a fixed-width font, otherwise the tabs do not line up correctly.

So, in my searching, I found a thread that says in interface builder, select the text view, pull up the font menu, and select your font. In order to KEEP it there, you have to throw a space into the text view.

Fine and dandy, this worked.

However, therein lies a problem. As soon as the space is deleted or otherwise removed, the formatting disappears. I have 2 text views that need this fixed-width font. One is editable, one is not, but they can both be set using code. (Set contents of text view…) This obviously deletes the space and gets rid of the formatting in the process.

I saw that user defaults can be bound to the font, but I’m not sure if this works, or if it’s worth the time compared to any other method. And the “set font of text view” just returns a bunch of errors in the log and doesn’t actually change the font.

I spent time looking around the apple developer pages, and this site, and cannot seem to find an answer. (Most people seem to be having trouble just setting the font :P) Is there any way to set the font from either within the code, or permantly in IB? Or is User Defaults my best option?


You can create a subclass of NSTextView in IB, and set that subclass as the class of your text view. In your subclass files, place the following awakefromnib method…

You’ll have to find an NSFont method that allows you to construct a valid NSFont to pass your desired font to the “setFont:” method on the last line.


Thanks! That did it!

Learn a little more each day :slight_smile:

Now then, on to fine tuning :stuck_out_tongue: Hopefully I’ll have something to show soon :slight_smile:


how about this one-liner?

call method "setFont:" of myTextView with parameter (call method "fontWithName:size:" of class "NSFont" with parameters {"Arial Black", 28})