How can I get a list of the available NSFont families?

前端 未结 6 1587
故里飘歌
故里飘歌 2021-02-05 10:15

How can I get a list of the available NSFont families, preferably with the fontName: equivalents.

6条回答
  •  -上瘾入骨i
    2021-02-05 10:28

    #import 
    
    @interface FontController : NSObject {
        IBOutlet NSTextField *text;
    }
    - (IBAction)takeFontFamilyFrom: (id)sender;
    - (IBAction)takeFontSizeFrom: (id)sender;
    - (IBAction)takeFontAttributeFrom: (id)sender;
    @end
    
    #import "FontController.h"
    
    NSFontManager *fm;
    
    @implementation FontController
    + (void)initialize
    {
        fm = [NSFontManager sharedFontManager];
    }
    - (IBAction)takeFontFamilyFrom: (id)sender
    {
        NSFont *font = [text font];
        font = [fm convertFont: font
                      toFamily: [sender stringValue]];
        [text setFont: font];
    }
    - (IBAction)takeFontSizeFrom: (id)sender
    {
        NSFont *font = [text font];
        font = [fm convertFont: font
                        toSize: [sender doubleValue]];
        [text setFont: font];
    }
    
    - (IBAction)takeFontAttributeFrom: (id)sender
    {
        NSFont *font = [text font];
        NSFontTraitMask attribute = [sender tag];
        if (NSOnState == [sender state])
        {
            font = [fm convertFont: font toHaveTrait: attribute];
        }
        else
        {
            font = [fm convertFont: font toNotHaveTrait: attribute];
        }
        [sender setState: ([fm traitsOfFont: font] & attribute)];
        [text setFont: font];
    }
    @end
    

提交回复
热议问题