开发者

How do I determine if a NSFont is installed on a Mac OSX machine in Objective-C?

开发者 https://www.devze.com 2023-01-28 07:19 出处:网络
Is there a method to开发者_StackOverflow中文版 check if a NSFont with string name is installed by the system?Check if it\'s present in this array:

Is there a method to开发者_StackOverflow中文版 check if a NSFont with string name is installed by the system?


Check if it's present in this array:

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

by calling

[fonts containsObject:@"Times"];

containsObject uses the isEqual: method to compare two objects. Because you know that every object in the fonts array is an NSString, you know that you'll get a YES if the array contains @"Times", and a NO if it doesn't.


you should refer to

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

for finding the font family (which might consist of several fonts (style)

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

and then you can check also for Bold, italic, etc (exact name)

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);
}

you might also want to use (But be sure to have the font name in right format) note that this i a bit more resource intensive

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

精彩评论

暂无评论...
验证码 换一张
取 消