问题
Helle every one !
I have added din.otf font in my iOS project. Then in my plist I have :
Now when I want to use it I juste have to write this line :
lalel.font = [UIFont fontWithName:@"din" size:12.f];
Am I right ? It doen't work at all ... Thanks !
回答1:
Try to list all fonts that are available on your device after importing by adding this piece of code :
(taken from : http://ajnaware.wordpress.com/2008/10/24/list-of-fonts-available-on-the-iphone/)
Legacy Answer
// List all fonts on iPhone
NSArray *familyNames = [[NSArray alloc] initWithArray:[UIFont familyNames]];
NSArray *fontNames;
NSInteger indFamily, indFont;
for (indFamily=0; indFamily<[familyNames count]; ++indFamily)
{
NSLog(@"Family name: %@", [familyNames objectAtIndex:indFamily]);
fontNames = [[NSArray alloc] initWithArray:
[UIFont fontNamesForFamilyName:
[familyNames objectAtIndex:indFamily]]];
for (indFont=0; indFont<[fontNames count]; ++indFont)
{
NSLog(@" Font name: %@", [fontNames objectAtIndex:indFont]);
}
[fontNames release];
}
[familyNames release];
And try to search for your font name.
// End Legacy Answer
I've found a way to load font at runtime which does not involve to add it on the .plist file.
+ (void)loadFontAtPath:(NSString*)path
{
NSData *data = [[NSFileManager defaultManager] contentsAtPath:path];
if(data == nil)
{
#ifdef DEBUG
NSLog(@"Failed to load font. Data at path is null path = %@", path);
#endif //ifdef Debug
return;
}
CFErrorRef error;
CGDataProviderRef provider = CGDataProviderCreateWithCFData((CFDataRef)data);
CGFontRef font = CGFontCreateWithDataProvider(provider);
if(!CTFontManagerRegisterGraphicsFont(font, &error)){
#ifdef DEBUG
CFStringRef errorDescription = CFErrorCopyDescription(error);
NSLog(@"Failed to load font: %@", errorDescription);
CFRelease(errorDescription);
return;
#endif //ifdef Debug
}
CFStringRef fontName = CGFontCopyFullName(font);
NSLog(@"Loaded: %@", fontName);
[[self sharedInstance] addFont:font withName:(NSString *)fontName];
CFRelease(fontName);
CFRelease(font);
CFRelease(provider);
}
+ (void) unloadFont:(NSString*) fontName
{
CFErrorRef error;
CGFontRef fontref = [[self sharedInstance] getFontWithName:fontName];
if(fontref)
{
CTFontManagerUnregisterGraphicsFont(fontref, &error);
[[self sharedInstance] removeFontWithName:(NSString *)fontName];
}
else
{
NSLog(@"WARNING: Font cannot be unloaded: %@", fontName);
}
}
And you can just use the name that NSLog(@"Loaded: %@", fontName); output.
来源:https://stackoverflow.com/questions/6828129/ios-font-loading-problem