Skip to content

Instantly share code, notes, and snippets.

@Koze
Last active May 30, 2018 12:22
Show Gist options
  • Save Koze/10550725 to your computer and use it in GitHub Desktop.
Save Koze/10550725 to your computer and use it in GitHub Desktop.
List of monospace font
// UIFontDescriptor on iOS 7 or later
NSDictionary *traitsAttributes = @{UIFontSymbolicTrait: @(UIFontDescriptorTraitMonoSpace)};
NSDictionary *fontAttributes = @{UIFontDescriptorTraitsAttribute: traitsAttributes};
UIFontDescriptor *fontDescriptor = [UIFontDescriptor fontDescriptorWithFontAttributes:fontAttributes];
NSArray *array = [fontDescriptor matchingFontDescriptorsWithMandatoryKeys:nil];
for (UIFontDescriptor *descriptor in array) {
NSLog(@"%@", descriptor.postscriptName);
// NSLog(@"%@", [descriptor objectForKey:UIFontDescriptorNameAttribute]);
// NSLog(@"%@", [descriptor objectForKey:UIFontDescriptorVisibleNameAttribute]);
// NSLog(@"%@", [descriptor objectForKey:UIFontDescriptorFamilyAttribute]);
// NSLog(@"%@", [descriptor objectForKey:UIFontDescriptorFaceAttribute]);
}
// NSLog(@"%@", array);
// CTFontDescriptorRef on iOS and OS X
NSDictionary *traitsAttributes = @{(id)kCTFontSymbolicTrait: @(kCTFontMonoSpaceTrait)};
NSDictionary *fontAttributes = @{(id)kCTFontTraitsAttribute: traitsAttributes};
CTFontDescriptorRef fontDescriptor = CTFontDescriptorCreateWithAttributes((CFDictionaryRef)fontAttributes);
CFArrayRef array = CTFontDescriptorCreateMatchingFontDescriptors(fontDescriptor, NULL);
CFIndex count = CFArrayGetCount(array);
for (int i=0; i<count; i++) {
CTFontDescriptorRef descriptor =CFArrayGetValueAtIndex(array, i);
NSLog(@"%@", CFBridgingRelease(CTFontDescriptorCopyAttribute(descriptor, kCTFontNameAttribute)));
// NSLog(@"%@", CFBridgingRelease(CTFontDescriptorCopyAttribute(descriptor, kCTFontDisplayNameAttribute)));
// NSLog(@"%@", CFBridgingRelease(CTFontDescriptorCopyAttribute(descriptor, kCTFontFamilyNameAttribute)));
// NSLog(@"%@", CFBridgingRelease(CTFontDescriptorCopyAttribute(descriptor, kCTFontStyleNameAttribute)));
}
// CFShow(array);
CFRelease(fontDescriptor);
CFRelease(array);
// NSFontDescriptor on OS X
NSDictionary *traitsAttributes = @{NSFontSymbolicTrait: @(NSFontMonoSpaceTrait)};
NSDictionary *fontAttributes = @{NSFontTraitsAttribute: traitsAttributes};
NSFontDescriptor *fontDescriptor = [NSFontDescriptor fontDescriptorWithFontAttributes:fontAttributes];
NSArray *array = [fontDescriptor matchingFontDescriptorsWithMandatoryKeys:nil];
for (NSFontDescriptor *descriptor in array) {
NSLog(@"%@", descriptor.postscriptName);
// NSLog(@"%@", [descriptor objectForKey:NSFontNameAttribute]);
// NSLog(@"%@", [descriptor objectForKey:NSFontVisibleNameAttribute]);
// NSLog(@"%@", [descriptor objectForKey:NSFontFamilyAttribute]);
// NSLog(@"%@", [descriptor objectForKey:NSFontFaceAttribute]);
}
// NSLog(@"%@", array);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment