简体   繁体   English

如何确定在Objective-C中的Mac OSX计算机上是否安装了NSFont?

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

有没有办法检查系统是否安装了带字符串名称的NSFont?

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. containsObject使用isEqual:方法来比较两个对象。 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. 因为你知道fonts数组中的每个对象都是一个NSString ,你知道如果数组包含@"Times"你将得到YES如果不包含,则得到NO

you should refer to 你应该参考

https://developer.apple.com/library/mac/documentation/TextFonts/Conceptual/CocoaTextArchitecture/FontHandling/FontHandling.html 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 ia bit more resource intensive 您可能还想使用(但一定要使用正确格式的字体名称)请注意,这会占用更多资源

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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