iOS 系統(tǒng)字體下載

開(kāi)發(fā)中,低版本系統(tǒng)打開(kāi) PDF、Word 可能會(huì)出現(xiàn)亂碼情況,很大的原因是低版本沒(méi)有該字體造成的。

1、在 mac 上找到字體 PostScript 名稱

Find 搜索字體冊(cè)


781540352115_.pic.jpg

找到對(duì)應(yīng)的字體 PostScript


791540352161_.pic_hd.jpg

注意:你可能會(huì)有一個(gè)疑問(wèn),為什么不把字體文件拖到項(xiàng)目中,在 plist 文件中添加,不就不用下載了嗎?
右鍵點(diǎn)擊該字體,在 Find 中顯示,我們發(fā)現(xiàn)該字體太大了,放到項(xiàng)目中反而得不償失。
再者,蘋果系統(tǒng)字體現(xiàn)在后是統(tǒng)一保存到系統(tǒng)文件下,所有 App 公用,也就是說(shuō),如果有其他 App 下載過(guò)該字體,咱們的也可以直接使用了。


811540538357_.pic_hd.jpg

2、話不多說(shuō),直接上代碼

新建系統(tǒng)字體下載管理類

@implementation FontNameManager
+ (instancetype)shareManager {
    static FontNameManager *manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[FontNameManager alloc] init];
    });
    return manager;
}

- (void)asynchronouslySetFontName:(NSString *)fontName
{
    UIFont* aFont = [UIFont fontWithName:fontName size:12.0];
    // If the font is already downloaded
    if (aFont && ([aFont.fontName compare:fontName] == NSOrderedSame || [aFont.familyName compare:fontName] == NSOrderedSame)) {
        // 已經(jīng)有改字體
        return;
    }
    
    // Create a dictionary with the font's PostScript name.
    NSMutableDictionary *attrs = [NSMutableDictionary dictionaryWithObjectsAndKeys:fontName, kCTFontNameAttribute, nil];
    
    // Create a new font descriptor reference from the attributes dictionary.
    CTFontDescriptorRef desc = CTFontDescriptorCreateWithAttributes((__bridge CFDictionaryRef)attrs);
    
    NSMutableArray *descs = [NSMutableArray arrayWithCapacity:0];
    [descs addObject:(__bridge id)desc];
    CFRelease(desc);
    
    __block BOOL errorDuringDownload = NO;
    CTFontDescriptorMatchFontDescriptorsWithProgressHandler( (__bridge CFArrayRef)descs, NULL,  ^(CTFontDescriptorMatchingState state, CFDictionaryRef progressParameter) {
        
        //NSLog( @"state %d - %@", state, progressParameter);
        
        double progressValue = [[(__bridge NSDictionary *)progressParameter objectForKey:(id)kCTFontDescriptorMatchingPercentage] doubleValue];
        
        if (state == kCTFontDescriptorMatchingDidBegin) {
            NSLog(@" 字體匹配成功 ");
        } else if (state == kCTFontDescriptorMatchingDidFinish) {
            dispatch_async( dispatch_get_main_queue(), ^ {
                
                // Log the font URL in the console
                CTFontRef fontRef = CTFontCreateWithName((__bridge CFStringRef)fontName, 0., NULL);
                CFStringRef fontURL = CTFontCopyAttribute(fontRef, kCTFontURLAttribute);
                NSLog(@"%@", (__bridge NSURL*)(fontURL));
                CFRelease(fontURL);
                CFRelease(fontRef);
                
                if (!errorDuringDownload) {
                    NSLog(@" 字體 %@ 下載完成 ", fontName);
                }
            });
        } else if (state == kCTFontDescriptorMatchingWillBeginDownloading) {
            NSLog(@" 字體開(kāi)始下載 ");
        } else if (state == kCTFontDescriptorMatchingDidFinishDownloading) {
            NSLog(@" 字體下載完成 ");
            dispatch_async( dispatch_get_main_queue(), ^ {
            });
        } else if (state == kCTFontDescriptorMatchingDownloading) {
            NSLog(@" 下載進(jìn)度 %.0f%% ", progressValue);
        } else if (state == kCTFontDescriptorMatchingDidFailWithError) {
            // An error has occurred.
            // Get the error message
            NSError *error = [(__bridge NSDictionary *)progressParameter objectForKey:(id)kCTFontDescriptorMatchingError];
            NSString *errorMessage = @"";
            if (error != nil) {
                errorMessage = [error description];
            } else {
                errorMessage = @"ERROR MESSAGE IS NOT AVAILABLE!";
            }
            // Set our flag
            errorDuringDownload = YES;
            
            dispatch_async( dispatch_get_main_queue(), ^ {
                NSLog(@"Download error: %@", errorMessage);
            });
        }
        return (bool)YES;
    });
}

在合適的位置,下載字體:

// 下載 黑體-簡(jiǎn) 細(xì)體
[[FontNameManager shareManager] asynchronouslySetFontName:@"STHeitiSC-Light"];

參考:https://developer.apple.com/library/archive/samplecode/DownloadFont/Listings/DownloadFont_ViewController_m.html

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容