2013-11-15 33 views

回答

7
- (NSString *)getFontPathOfFont:(NSFont *)font 
{ 

    CTFontDescriptorRef fontRef = CTFontDescriptorCreateWithNameAndSize ((CFStringRef)[font fontName], [font pointSize]); 
    CFURLRef url = (CFURLRef)CTFontDescriptorCopyAttribute(fontRef, kCTFontURLAttribute); 
    NSString *fontPath = [NSString stringWithString:[(NSURL *)CFBridgingRelease(url) path]]; 
    return fontPath; 
} 

上面的代碼解決了我的問題 - 我只是要送的字體

0
+(NSString *)getFontPath : (NSString *)fontName 
{ 
    @autoreleasepool { 
     NSArray *libraryPaths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSAllDomainsMask, YES); 
     NSString *fontPath = nil; 
     for (NSString *path in libraryPaths) { 
      NSString *currentFontPath = [[path stringByAppendingPathComponent:@"Fonts"] stringByAppendingPathComponent:fontName]; 
      if ([[NSFileManager defaultManager] fileExistsAtPath:currentFontPath]) { 
       fontPath = currentFontPath; 
       break; 
      } 
     } 
     return fontPath; 
    } 
} 

[AppDelegate getFontPath:@"Arial.ttf"]; 
output : /Library/Fonts/Arial.ttf 
+0

在這一解決方案,我必須重視的.ttf或雜項文件結尾。我只是想送字體系列的名稱,如 宋體或黑體或格魯吉亞 例如 NSFont * F = [myTF字體] [AppDelegate getFontPath:f.familyName]; – user226372

相關問題