iOS开发:导入字体库

来源:互联网 发布:在线制作淘宝商品图片 编辑:程序博客网 时间:2024/05/14 10:49

使用自定义字体


  • 方式一:本地字体导入到工程目录使用

    1. 首先,去找个字体包下载下来,我用的是方正的兰亭特黑简体(图1),2.5M大小
      图1

    2. 拖到工程目录下,确保在Copy Bundle Resources里能看到这个包(图2)
      图2

    3. 配置plist:在info.plist里加入字段Fonts provided by application添加一个item为字体包的名字(图3)
      图3

    4. 有人博客里说接下来就打开字体包记下名称(图4)就可以作为字体名用了
      图4

    5. 我这个不行,我打印了下,系统里当前字体列表
      NSArray * fontsArray = [NSArray alloc] initWithArray: [UIFont familyNames]]
      for (NSString *font in fontsArray) {
      NSLog(@"Font Name = %@\n",font);
      }

    6. 找到导入的字体(图6)
      图6

    7. 然后就可以用了
      [UIFont fontWithName:@"FZLanTingHei-H-GBK" size:17]

  • 方式二:使用从服务器下载的字体
    1. 下载字体到本地

    2. 新建UIFont类扩展UIFont+customfont

    3. 实现方法
      +(UIFont*)customFontWithPath:(NSString*)path size:(CGFloat)size
      {
      NSURL *fontUrl = [NSURL fileURLWithPath:path];
      CGDataProviderRef fontDataProvider =CGDataProviderCreateWithURL((__bridge CFURLRef)fontUrl);
      CGFontRef fontRef = CGFontCreateWithDataProvider(fontDataProvider);
      CGDataProviderRelease(fontDataProvider);
      CTFontManagerRegisterGraphicsFont(fontRef, NULL);
      NSString *fontName = CFBridgingRelease(CGFontCopyPostScriptName(fontRef));
      UIFont *font = [UIFont fontWithName:fontName size:size];
      CGFontRelease(fontRef);
      return font;
      }


    4. 你懂的
      [UIFont customFontWithPath:fontFilePath size:20];
0 0
原创粉丝点击