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;
}
精彩评论