ios 9.2以上自定義中文字體掉進的坑(三種方法設置字體)
知識
07-18
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"];
- }
![](https://pic.pimg.tw/zzuyanan/1488615166-1259157397.png)
![](https://pic.pimg.tw/zzuyanan/1482887990-2595557020.jpg)
TAG:程序員小新人學習 |