iOS字体加载问题

发布于 2024-11-26 01:20:21 字数 266 浏览 1 评论 0 原文

大家好!

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

在此处输入图像描述

现在,当我想使用它时,我只需编写这一行:

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

我说得对吗?它根本不起作用... 谢谢 !

Helle every one !

I have added din.otf font in my iOS project.
Then in my plist I have :

enter image description here

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 !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧情别恋 2024-12-03 01:20:21

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

取自: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];

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

// End Legacy Answer

我找到了一种在运行时加载字体的方法,该方法不涉及将其添加到 .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); 的名称输出。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文