在OSX中是否有替代Windows的CreateFont(..)
功能?在OSX中查找指定特徵的字體
我試着使用NSFontDescriptor
及其matchingFontDescriptorsWithMandatoryKeys:
方法,但這種方法並不能找到「最接近」字體,它找到匹配的特性,也可以返回nil
。
有什麼辦法在OSX中按指定特徵(如CreateFont)找到最接近的字體?
更新與NSFontDescriptor
奇怪的事情發生..
我有兩段代碼:
可可:
NSDictionary* fontTraits = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:NSFontSansSerifClass], NSFontSymbolicTrait,
[NSNumber numberWithFloat:0.4], NSFontWidthTrait,
nil];
NSDictionary* fontAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
fontTraits, NSFontTraitsAttribute,
nil];
NSFontDescriptor* fontDescriptor = [NSFontDescriptor fontDescriptorWithFontAttributes:fontAttributes];
NSArray* matchedDescriptors = [fontDescriptor matchingFontDescriptorsWithMandatoryKeys:nil];
而且使用CoreText API:
CFMutableDictionaryRef fontTraits = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
float weight = 0.4;
CFNumberRef fontWeight = CFNumberCreate(kCFAllocatorDefault, kCFNumberFloat32Type, &weight);
CFDictionaryAddValue(fontTraits, kCTFontWeightTrait, fontWeight);
int symbolicTraits = kCTFontSansSerifClass;
CFNumberRef fontSymbolicTraits = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &symbolicTraits);
CFDictionaryAddValue(fontTraits, kCTFontSymbolicTrait, fontSymbolicTraits);
CFMutableDictionaryRef fontAttributes = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
CFDictionaryAddValue(fontAttributes, kCTFontTraitsAttribute, fontTraits);
CTFontDescriptorRef fontDescriptor = CTFontDescriptorCreateWithAttributes(fontAttributes);
CFArrayRef matchedDescriptors = CTFontDescriptorCreateMatchingFontDescriptors(fontDescriptor, 0);
我在這兩種情況下都創建了相同的字體描述符,但在第一種情況下,matchedDescriptors
是nil
,而CoreText API中有一些字體在matchedDescriptors
中。這是一個錯誤嗎?
但一般來說,如果我通過nil
到XXXMatchingFontDescriptorsXXX
作爲強制屬性,它是否應返回至少一個描述符?