Last active
August 15, 2020 13:10
-
-
Save orklann/3632b7ee7b0c32434781130617004dca to your computer and use it in GitHub Desktop.
Get Font Path from NSFont in Cococa
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// main.m | |
// FontPath | |
// | |
// Created by Aaron Elkins on 8/15/20. | |
// Copyright © 2020 Aaron Elkins. All rights reserved. | |
// | |
#import <Foundation/Foundation.h> | |
#import <Cocoa/Cocoa.h> | |
NSString *getFontPathOfFont(NSFont *font) { | |
CTFontDescriptorRef fontRef = CTFontDescriptorCreateWithNameAndSize ((CFStringRef)[font fontName], [font pointSize]); | |
NSFontDescriptor* newFontRef = [(__bridge NSFontDescriptor*)fontRef fontDescriptorByAddingAttributes:@{ NSFontTraitsAttribute: @{ NSFontWeightTrait: @(4)} }]; | |
CFURLRef url = (CFURLRef)CTFontDescriptorCopyAttribute((CTFontDescriptorRef)newFontRef, kCTFontURLAttribute); | |
NSString *fontPath = [NSString stringWithString:[(NSURL *)CFBridgingRelease(url) path]]; | |
return fontPath; | |
} | |
int main(int argc, const char * argv[]) { | |
@autoreleasepool { | |
NSFont *font = [NSFont fontWithName:@"Roboto" size:13]; | |
NSFont *newFont = [[NSFontManager sharedFontManager] convertWeight:(BOOL)YES | |
ofFont:(NSFont *)font]; | |
NSLog(@"%@", [[NSFontManager sharedFontManager] availableFonts]); | |
NSLog(@"%@", getFontPathOfFont(newFont)); | |
} | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment