iOS字体加载问题

时间:2011-07-26 09:43:08

标签: ios uifont custom-font

帮助每一个人!

我在iOS项目中添加了din.otf字体。 然后在我的plist中我有:

enter image description here

现在,当我想使用它时,我必须写下这一行:

lalel.font = [UIFont fontWithName:@"din" size:12.f];

我是对的吗?它根本不起作用...... 谢谢!

1 个答案:

答案 0 :(得分:2)

尝试通过添加以下代码列出导入后设备上可用的所有字体:

(取自:http://ajnaware.wordpress.com/2008/10/24/list-of-fonts-available-on-the-iphone/

遗产答案

// 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];

并尝试搜索您的字体名称。

//结束传统答案

我找到了一种在运行时加载字体的方法,不需要将其添加到.plist文件中。

+ (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);
    }
}

你可以使用NSLog的名称(@“Loaded:%@”,fontName);输出