How to determine if NSFont is installed on a Mac OSX computer in Objective-C?

Is there a way to check if NSFont is installed with a string name in the system?

+3
source share
2 answers

Check if this array is present in this array:

NSArray *fonts = [[NSFontManager sharedFontManager] availableFontFamilies];

causing

[fonts containsObject:@"Times"];

containsObjectuses a method isEqual:to compare two objects. Since you know what every object in the array fontsis NSString, you know what you will get YESif the array contains @"Times", and NOif it is not.

+4
source

you must turn to

https://developer.apple.com/library/mac/documentation/TextFonts/Conceptual/CocoaTextArchitecture/FontHandling/FontHandling.html

( (style)

NSFontDescriptor *helveticaNeueFamily =
    [NSFontDescriptor fontDescriptorWithFontAttributes:
        @{ NSFontFamilyAttribute: @"Helvetica Neue" }];
NSArray *matches =
    [helveticaNeueFamily matchingFontDescriptorsWithMandatoryKeys: nil];

, .. ( )

NSFontDescriptor *fontDescriptor =
    [NSFontDescriptor fontDescriptorWithFontAttributes:
        @{ NSFontNameAttribute: @"Bank Gothic Medium" }];
NSArray *matches =
    [fontDescriptor matchingFontDescriptorsWithMandatoryKeys: nil];

like

- (BOOL)isFontNameInstalledInSystem {

    if (self.fontName == nil) {
        return NO;
    }

    NSFontDescriptor *fontDescriptor = [NSFontDescriptor fontDescriptorWithFontAttributes:@{NSFontNameAttribute:self.fontName}];
    NSArray *matches = [fontDescriptor matchingFontDescriptorsWithMandatoryKeys: nil];

    return ([matches count] > 0);
}

( ) ,

- (BOOL)isFontNameInstalledInSystem {
    return ([NSFont fontWithName:self.fontName size:[NSFont systemFontSize]]) != nil;
}
0

Source: https://habr.com/ru/post/1778215/


All Articles