2010-04-24 17 views

risposta

10

Sembra che CTFontManagerCreateFontDescriptorsFromURL sostituisca il testo principale.

+2

E lo fa per [il codice più corto del percorso core grafico] (https: //gist.github. com/1.696.100). –

18

È possibile ottenere un CTFontRef da un file di font da passare attraverso un CGFontRef:

CFURLRef url = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, CFSTR("/path/to/font"), kCFURLPOSIXPathStyle, false); 
CGDataProviderRef dataProvider = CGDataProviderCreateWithURL(url); 
CGFontRef theCGFont = CGFontCreateWithDataProvider(dataProvider); 
CTFontRef theCTFont = CTFontCreateWithGraphicsFont(theCGFont); 
CFRelease(theCGFont); 
CFRelease(dataProvider); 
CFRelease(url); 

// do something with the CTFontRef here 

CFRelease(theCTFont); 
+0

Questo non funziona con Snow Leopard (confermato da Apple) e devi usare 'ATSFontActivateFromMemory()' solo su quella versione di OS X. – trojanfoe

+0

Impossibile confermare questo. Funziona bene su Snow Leopard. – Andreas

4
NSURL *fontURL = [[NSBundle mainBundle] URLForResource:@"Crystal" withExtension:@"ttf"]; 
    assert(fontURL); 
    CFErrorRef error = NULL; 
    if (!CTFontManagerRegisterFontsForURL((__bridge CFURLRef)fontURL, kCTFontManagerScopeProcess, &error)) 
    { 
     CFShow(error); 
     abort(); 
    } 
+1

questo ha funzionato per me, grazie! – MiMo