iOS_動態(tài)添加中文字體庫

ios本身支持的字體庫有限,如果用到比較偏門的萌萌噠字體就更不支持了,本文簡單敘述一下iOS添加字體庫的幾種方式,希望可以幫助更多的人,節(jié)省更多時間,不勝榮幸

我們項(xiàng)目的需求:?

WebView加載的文章需要支持楷體,iOS本身沒有這個字體庫,所以需要添加字體庫才能支持

方式一:

簡單粗暴型: 將字體庫資源直接添加到工程中,在plist文件配置后方可使用

方式二:

調(diào)用蘋果提供的字體API,將字體庫下載到手機(jī)系統(tǒng)中,相比方式一的優(yōu)點(diǎn)是可以減少工程的體積,因?yàn)橐粋€字體庫通常在10M以上,并且可以根據(jù)服務(wù)端返回信息動態(tài)添加字體庫

方式一具體步驟如下:

首先需要找到字體庫資源:

在Launchpad中搜索“字體冊”,

找到字體show in finder下載字體庫 ?

將字體庫導(dǎo)入工程中

在infoplist進(jìn)行設(shè)置


使用方式:UIFont *font = [UIFont fontWithName:@"" size:12];

方式二 : 動態(tài)添加字體庫

iOS6以后蘋果就開始支持動態(tài)下載中文字體已供應(yīng)用中展示個性字體的需求,由于下載的時候需要使用的名字是PostScript名稱,需要使用Mac內(nèi)自帶的應(yīng)用“字體冊“來獲得相應(yīng)字體的PostScript名稱。如下顯示了從”字體冊“中獲取《娃娃體-繁 常規(guī)體》字體的PostScript名稱的截圖


代碼部分:

- (void)requestFont

{

UIFont* aFont = [UIFont fontWithName:fontName size:12.0];

if (aFont && ([aFont.fontName compare:fontName] == NSOrderedSame

|| [aFont.familyName compare:fontName] == NSOrderedSame))

{

return;

} else

{

// 用字體的 PostScript 名字創(chuàng)建一個 Dictionary

NSMutableDictionary *attrs = [NSMutableDictionary dictionaryWithObjectsAndKeys:fontName, kCTFontNameAttribute, nil];

// 創(chuàng)建一個字體描述對象 CTFontDescriptorRef

CTFontDescriptorRef desc = CTFontDescriptorCreateWithAttributes((__bridge CFDictionaryRef)attrs);

// 將字體描述對象放到一個 NSMutableArray 中

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) {

double progressValue = [[(__bridge NSDictionary *)progressParameter objectForKey:(id)kCTFontDescriptorMatchingPercentage] doubleValue];

if (state == kCTFontDescriptorMatchingDidBegin) {

NSLog(@" 字體已經(jīng)匹配 ");

} else if (state == kCTFontDescriptorMatchingDidFinish) {

if (!errorDuringDownload)

{

NSLog(@" 字體 %@ 下載完成 ", fontName);

dispatch_async( dispatch_get_main_queue(),

^ {

// 可以在這里修改 UI 控件的字體

self.btn.titleLabel.font = [UIFont fontWithName:fontName size:15];

[self.btn setTitle:@"按鈕按鈕" forState:UIControlStateNormal];

});

}

} else if (state == kCTFontDescriptorMatchingWillBeginDownloading) {

NSLog(@" 字體開始下載 ");

} else if (state == kCTFontDescriptorMatchingDidFinishDownloading)

{

NSLog(@" 字體下載完成 ");

dispatch_async( dispatch_get_main_queue(),

^ {

// 可以在這里修改 UI 控件的字體

self.btn.titleLabel.font = [UIFont fontWithName:fontName size:15];

[self.btn setTitle:@"按鈕按鈕" forState:UIControlStateNormal];

});

} else if (state == kCTFontDescriptorMatchingDownloading) {

NSLog(@" 下載進(jìn)度 %.0f%% ", progressValue);

} else if (state == kCTFontDescriptorMatchingDidFailWithError) {

NSError *error = [(__bridge NSDictionary *)progressParameter objectForKey:(id)kCTFontDescriptorMatchingError];

if (error != nil)

{

_errorMessage = [error description];

} else {

_errorMessage = @"ERROR MESSAGE IS NOT AVAILABLE!";

}

// 設(shè)置標(biāo)志

errorDuringDownload = YES;

NSLog(@" 下載錯誤: %@", _errorMessage);

}

return (BOOL)YES;

});

}

}

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

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

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