【问题标题】:Dynamic custom fonts loader in iOSiOS 中的动态自定义字体加载器
【发布时间】:2012-05-05 22:40:53
【问题描述】:

我已经知道如何从here 在 iPhone 应用程序中将自定义字体加载到我的项目中 我想问是否有办法从代码中做到这一点?我的问题是我的应用程序中有一个资源文件夹,我有一个字体文件名,我们称之为“myfont.ttf”。

我想获取一个 ttf 文件并将其放入 从代码 的 plist 文件中,而且我还想知道 fontWithName:size: 方法的显示名称。有没有办法做到这一点?

【问题讨论】:

    标签: iphone objective-c ios uifont


    【解决方案1】:

    如果您正在下载 TTF 文件,那么您可以执行以下操作以使用 iOS 字体管理器注册您的自定义字体,这段代码还负责 TTF 文件更新(字体更新):

        +(void)registerFontsAtPath:(NSString *)ttfFilePath
        {
            NSFileManager * fileManager = [NSFileManager defaultManager];
    
            if ([fileManager fileExistsAtPath:ttfFilePath] == YES)
            {
                [UIFont familyNames];//This is here for a bug where font registration API hangs for forever.
    
                //In case of TTF file update : Fonts are already registered, first de-register them from Font Manager
                CFErrorRef cfDe_RegisterError;
               bool fontsDeregistered = CTFontManagerUnregisterFontsForURL((__bridge CFURLRef)[NSURL fileURLWithPath:ttfFilePath], kCTFontManagerScopeNone, &cfDe_RegisterError);
    
    
                //finally register the fonts with Font Manager,
                CFErrorRef cfRegisterError;
                bool fontsRegistered= CTFontManagerRegisterFontsForURL((__bridge CFURLRef)[NSURL fileURLWithPath:ttfFilePath], kCTFontManagerScopeNone, &cfRegisterError);
             }
         }
    

    您可以检查注册和注销状态的布尔值和错误。

    【讨论】:

      【解决方案2】:

      这是一个较老的问题,但无论如何,如果其他人遇到这个问题,这里有一种方法。

      
      + (void)loadFontAtPath:(NSString*)path{
          NSData *data = [[NSFileManager defaultManager] contentsAtPath:path];
          if(data == nil){
              NSLog(@"Failed to load font. Data at path is null");
              return;
          }
          CFErrorRef error;
          CGDataProviderRef provider = CGDataProviderCreateWithCFData((CFDataRef)data);
          CGFontRef font = CGFontCreateWithDataProvider(provider);
          if(!CTFontManagerRegisterGraphicsFont(font, &error)){
              CFStringRef errorDescription = CFErrorCopyDescription(error);
              NSLog(@"Failed to load font: %@", errorDescription);
              CFRelease(errorDescription);
          }
          CFRelease(font);
          CFRelease(provider);
      }
      

      这将在运行时指定的路径加载字体,然后您可以像平常一样使用它,而无需将其添加到 plist。

      【讨论】:

      • 这有效,它将字体应用于标签,但没有注册。
      • @pixelrevision CTFontManagerRegisterGraphicsFont 是否将字体永久注册到设备
      • CGFontRef 字体 = CGFontCreateWithDataProvider(provider);应用程序永远挂在这条线上。 (iOS 10.2)
      【解决方案3】:

      是的,你可以。但是您必须大量使用 CoreText 和/或 CoreGraphics。

      Zynga 有一个很好的课程可以帮助你做到这一点: https://github.com/zynga/FontLabel

      示例项目展示了如何在不使用 .plist 的情况下从包中加载 .ttf 文件并在应用程序中使用这些字体。

      代码是有效的,从一开始就很好。

      编辑:以前的方法使用 CoreGraphics,这很好,但使用 Core Text 更好。 我找到了这个问题的有趣答案:How can you load a font (TTF) from a file using Core Text?

      如果您没有使用 CoreText 框架的经验,请阅读official introduction inside the Apple documentation

      【讨论】:

        猜你喜欢
        • 2020-12-10
        • 2012-08-08
        • 1970-01-01
        • 1970-01-01
        • 2015-06-30
        • 1970-01-01
        • 2018-02-23
        • 1970-01-01
        • 2013-06-10
        相关资源
        最近更新 更多