當前位置:
首頁 > 知識 > ios 9.2以上自定義中文字體掉進的坑(三種方法設置字體)

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、第三種就是下載蘋果支持的中文字體。

一般都是用下面的方法:

  1. -(BOOL)isFontDownloaded:(NSString *)fontName {
  2. UIFont* aFont = [UIFont fontWithName:fontName size:20.0];
  3. if (aFont && ([aFont.fontName compare:fontName] == NSOrderedSame
  4. || [aFont.familyName compare:fontName] == NSOrderedSame)) {
  5. return YES;
  6. } else {
  7. return NO;
  8. }
  9. }
  10. -(void)downloadFontWithPostScriptName:(NSString *)postScriptName{
  11. if ([self isFontDownloaded:postScriptName]) {
  12. return;
  13. }
  14. NSMutableDictionary *attrsDictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:postScriptName,kCTFontNameAttribute, nil];
  15. CTFontDescriptorRef descriptor = CTFontDescriptorCreateWithAttributes((__bridge CFDictionaryRef)attrsDictionary);
  16. NSMutableArray *descriptorArray = [NSMutableArray array];
  17. [descriptorArray addObject:(__bridge id)descriptor];
  18. CFRelease(descriptor);
  19. __block BOOL errorDuringDownload = NO;
  20. CTFontDescriptorMatchFontDescriptorsWithProgressHandler((__bridge CFArrayRef)descriptorArray, NULL, ^bool(CTFontDescriptorMatchingState state, CFDictionaryRef _Nonnull progressParameter) {
  21. double progressValue = [[(__bridge NSDictionary *)progressParameter objectForKey:(id)kCTFontDescriptorMatchingPercentage] doubleValue];
  22. if (state == kCTFontDescriptorMatchingDidBegin) {
  23. NSLog(@"字體已經匹配");
  24. }else if (state == kCTFontDescriptorMatchingDidFinish){
  25. if (!errorDuringDownload) {
  26. NSLog(@"字體%@下載完成",postScriptName);
  27. dispatch_async(dispatch_get_main_queue(), ^{
  28. // 在此修改UI控制項的字體
  29. testInfo.font=[UIFont fontWithName:@"DFWaWaSC-W5" size:20.0];
  30. [testInfo setText:@"測試一下而已"];
  31. });
  32. }
  33. }else if (state == kCTFontDescriptorMatchingWillBeginDownloading){
  34. NSLog(@"字體開始下載");
  35. }else if (state == kCTFontDescriptorMatchingDidFinishDownloading){
  36. NSLog(@"字體下載完成");
  37. }else if (state == kCTFontDescriptorMatchingDownloading){
  38. NSLog(@"下載進度%.0f%%",progressValue);
  39. }else if (state == kCTFontDescriptorMatchingDidFailWithError){
  40. NSError *error = [(__bridge NSDictionary *)progressParameter objectForKey:(id)kCTFontDescriptorMatchingError];
  41. NSString *errorMesage = [NSString string];
  42. if (error != nil) {
  43. errorMesage = [error description];
  44. } else {
  45. errorMesage = @"ERROR MESSAGE IS NOT AVAILABLE!";
  46. }
  47. // 設置下載失敗標誌
  48. errorDuringDownload = YES;
  49. NSLog(@"下載錯誤:%@",errorMesage);
  50. }
  51. return (bool)YES;
  52. });
  53. }

坑來了!

第一次下載字體,設置是有效果的,如果你關閉了ViewController,從新進來,大概要跑這樣的代碼:

  1. if([self isFontDownloaded:@"DFWaWaSC-W5"])
  2. {
  3. testInfo.font=[UIFont fontWithName:@"DFWaWaSC-W5" size:20.0];
  4. [testInfo setText:@"我是王"];
  5. }else
  6. {
  7. NSLog(@"==========no==yes==");
  8. [self downloadFontWithPostScriptName:@"DFWaWaSC-W5"];
  9. }

如果已經有了,我們直接使用就應該可以了!我是這麼想的。直接在viewLoaded就這麼用了。抱歉!這麼玩「我是王」顯示是顯示了,但是沒有效果。

然後你列印這個font,沒毛病,有內容!坑啊~~~~

怎麼辦呢?其實要這麼搞,才出效果,代碼如下:

  1. if([self isFontDownloaded:@"DFWaWaSC-W5"])
  2. {
  3. dispatch_async(dispatch_get_main_queue(), ^{
  4. // 在此修改UI控制項的字體
  5. testInfo.font=[UIFont fontWithName:@"DFWaWaSC-W5" size:20.0];
  6. [testInfo setText:@"我是王"];
  7. });
  8. }else
  9. {
  10. NSLog(@"==========no==yes==");
  11. [self downloadFontWithPostScriptName:@"DFWaWaSC-W5"];
  12. }

ios 9.2以上自定義中文字體掉進的坑(三種方法設置字體)

喜歡這篇文章嗎?立刻分享出去讓更多人知道吧!

本站內容充實豐富,博大精深,小編精選每日熱門資訊,隨時更新,點擊「搶先收到最新資訊」瀏覽吧!


請您繼續閱讀更多來自 程序員小新人學習 的精彩文章:

Http客戶端工具類

TAG:程序員小新人學習 |