简体   繁体   中英

Keep NSTextView's Font From Changing

I thought this would be straight forward, but it looks like I was wrong. Basically, all I'm trying to do is keep the font from changing to the Apple default: Helvetica Regular 12pt.

I've made a subclass of NSDocument and in my implementation file I have the following method:

- (void)windowControllerDidLoadNib:(NSWindowController*)aController
{
    [super windowControllerDidLoadNib:aController];

    if(attrString)
    {
        [[textView textStorage] setAttributedString:attrString];
        [[textView textStorage] setFont:[NSFont fontWithName:@"Menlo Bold" size:24]];
    }

This method works all right when I open a file, but if I delete all of the text and then type again, the font resets to... Helvetica Regular 12pt... All I want is to keep the font and size as I specified it for the entire life of the program.

You need to set the typing attributes of the text view to contain your font for the key NSFontAttributeName.

However, I would go a step further. If you know you NEVER want a certain font in your model (NSTextStorage -- the backing store for NSTextView), simply subclass NSTextStorage and override the attribute setters and getters. NSTextView gives the user access to font menus and copy/paste will still allow certain fonts in. The only way to truly guarantee it never enters your text view is to never allow it into the model.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM