ios 9.2以上自定义中文字体掉进的坑(三种方法设置字体)


1、最基本的就是遍历手机上目前有的字体,但是这些字体基本都是英文的。


2、第二种就是把ttf文件放到项目里面,如果你查网络上的文章,你会发现要修改info.plist,添加Fonts provided by application。然后把item添加上。

但是我在遍历系统支持的字体,愣是没有?这个坑是怎么回事?

其实需要修改Fonts provided by application 成UIAppFonts。就OK了。浪费了几个小时!(当然你要注意build phase里面的copy bundle source要有字体)


3、第三种就是下载苹果支持的中文字体。

一般都是用下面的方法:

-(BOOL)isFontDownloaded:(NSString *)fontName {
    UIFont* aFont = [UIFont fontWithName:fontName size:20.0];
    if (aFont && ([aFont.fontName compare:fontName] == NSOrderedSame
                  || [aFont.familyName compare:fontName] == NSOrderedSame)) {
        return YES;
    } else {
        return NO;
    }
}


-(void)downloadFontWithPostScriptName:(NSString *)postScriptName{
    if ([self isFontDownloaded:postScriptName]) {
        return;
    }
    NSMutableDictionary *attrsDictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:postScriptName,kCTFontNameAttribute, nil];
    CTFontDescriptorRef descriptor = CTFontDescriptorCreateWithAttributes((__bridge CFDictionaryRef)attrsDictionary);
    NSMutableArray *descriptorArray = [NSMutableArray array];
    [descriptorArray addObject:(__bridge id)descriptor];
    CFRelease(descriptor);
    
    __block BOOL errorDuringDownload = NO;
    CTFontDescriptorMatchFontDescriptorsWithProgressHandler((__bridge CFArrayRef)descriptorArray, NULL, ^bool(CTFontDescriptorMatchingState state, CFDictionaryRef  _Nonnull progressParameter) {
        double progressValue = [[(__bridge NSDictionary *)progressParameter objectForKey:(id)kCTFontDescriptorMatchingPercentage] doubleValue];
        if (state == kCTFontDescriptorMatchingDidBegin) {
            NSLog(@"字体已经匹配");
        }else if (state == kCTFontDescriptorMatchingDidFinish){
            if (!errorDuringDownload) {
                NSLog(@"字体%@下载完成",postScriptName);
                dispatch_async(dispatch_get_main_queue(), ^{
                    // 在此修改UI控件的字体
                    
                    testInfo.font=[UIFont fontWithName:@"DFWaWaSC-W5" size:20.0];
                    [testInfo setText:@"测试一下而已"];
                    
                });
            }
        }else if (state == kCTFontDescriptorMatchingWillBeginDownloading){
            NSLog(@"字体开始下载");
        }else if (state == kCTFontDescriptorMatchingDidFinishDownloading){
            NSLog(@"字体下载完成");
        }else if (state == kCTFontDescriptorMatchingDownloading){
            NSLog(@"下载进度%.0f%%",progressValue);
        }else if (state == kCTFontDescriptorMatchingDidFailWithError){
            NSError *error = [(__bridge NSDictionary *)progressParameter objectForKey:(id)kCTFontDescriptorMatchingError];
            NSString *errorMesage = [NSString string];
            if (error != nil) {
                errorMesage = [error description];
            } else {
                errorMesage = @"ERROR MESSAGE IS NOT AVAILABLE!";
            }
            // 设置下载失败标志
            errorDuringDownload = YES;
            NSLog(@"下载错误:%@",errorMesage);
        }
        return (bool)YES;
    });
    
}

坑来了!

第一次下载字体,设置是有效果的,如果你关闭了ViewController,从新进来,大概要跑这样的代码:

   if([self isFontDownloaded:@"DFWaWaSC-W5"])
      {
         
              testInfo.font=[UIFont fontWithName:@"DFWaWaSC-W5" size:20.0];
              [testInfo setText:@"我是王"];
          

      }else
      {
          NSLog(@"==========no==yes==");
          [self downloadFontWithPostScriptName:@"DFWaWaSC-W5"];
      }
    


如果已经有了,我们直接使用就应该可以了!我是这么想的。直接在viewLoaded就这么用了。抱歉!这么玩“我是王”显示是显示了,但是没有效果。

然后你打印这个font,没毛病,有内容!坑啊~~~~


怎么办呢?其实要这么搞,才出效果,代码如下:

   if([self isFontDownloaded:@"DFWaWaSC-W5"])
      {
          dispatch_async(dispatch_get_main_queue(), ^{
              // 在此修改UI控件的字体
              
              testInfo.font=[UIFont fontWithName:@"DFWaWaSC-W5" size:20.0];
              [testInfo setText:@"我是王"];
              
          });

      }else
      {
          NSLog(@"==========no==yes==");
          [self downloadFontWithPostScriptName:@"DFWaWaSC-W5"];
      }
    




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

了悟生死大事

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值