Question

I am trying to get the Sample text of a font.This is my coding.

    NSString *fontFilePath=@"/Volumes/Work/Mac/Fonts/FONT FOLDER/Times New Roman Bold.ttf";
    CFStringRef aCFString = (CFStringRef)fontFilePath;
    CTFontRef fontRef = CTFontCreateWithName(aCFString, 0.0,NULL);           
    CFStringRef sampleText=CTFontCopyName(fontRef,kCTFontSampleTextNameKey);
    NSLog(@"%@",sampleText);

It returns null as the result. But for the kCTFontCopyrightNameKey, kCTFontLicenseURLNameKey etc.... It returns the correct value. For only the kCTFontSampleTextNameKey it returns null. May I know how can I able to get the sampletext of the given font.

Was it helpful?

Solution

That font just doesn't have the requested value. Either handle that case or edit the font and add your own value.

If you want to add the missing value into font, you need to use some font editor. Sorry, don't know any (but bet google search might find a few).

If you want to handle the case in your own code, for example for built-in system fonts, here's some sample code:

CFStringRef sampleText = CTFontCopyName(fontRef,kCTFontSampleTextNameKey);
NSLog(@"%@", [NSString stringWithFormat:@"%@", (NSString *)sampleText ? (NSString *)sampleText : @""],
if (sampleText) CFRelease(sampleText);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top