IOS字体下载
結(jié)合書本與蘋果官方給的例子后,總結(jié)下下載的方法。
蘋果給我們提供了很多漂亮的字體,只是有些字體設(shè)備并沒有內(nèi)置,需要我們?nèi)ハ螺d才行。
系統(tǒng)提供給我們的字體名我們可以通過mac系統(tǒng)提供的字體冊(cè)來查閱。
得到我們想要的字體后就可以在我們的設(shè)備上進(jìn)行下載了。這里要說一下,設(shè)備字體下載后是所有應(yīng)用都可以使用的,而且字體的目錄并不是我們APP的目錄,因此并不會(huì)增大我們應(yīng)用所需的空間。
這里結(jié)合著蘋果官方所給例子來簡(jiǎn)述一下(官方例子):
事例中給我們預(yù)定了幾種字體來讓我們下載
1 - (void)viewDidLoad 2 { 3 [super viewDidLoad]; 4 5 self.fontNames = [[NSArray alloc] initWithObjects: 6 @"STXingkai-SC-Light", 7 @"DFWaWaSC-W5", 8 @"FZLTXHK--GBK1-0", 9 @"STLibian-SC-Regular", 10 @"LiHeiPro", 11 @"HiraginoSansGB-W3", 12 nil]; 13 self.fontSamples = [[NSArray alloc] initWithObjects: 14 @"漢體書寫信息技術(shù)標(biāo)準(zhǔn)相", 15 @"容檔案下載使用界面簡(jiǎn)單", 16 @"支援服務(wù)升級(jí)資訊專業(yè)制", 17 @"作創(chuàng)意空間快速無(wú)線上網(wǎng)", 18 @"兙兛?jī)羶纼膬艈憝櫦H", 19 @"㈠㈡㈢㈣㈤㈥㈦㈧㈨㈩", 20 nil]; 21 }然后在表示圖的didSelectedRowAtIndexPath委托中來調(diào)用驗(yàn)證字體的方法
1 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 2 { 3 [self asynchronouslySetFontName:_fontNames[indexPath.row]]; 4 5 // Dismiss the keyboard in the text view if it is currently displayed 6 if ([self.fTextView isFirstResponder]) 7 [self.fTextView resignFirstResponder]; 8 }重點(diǎn)來看asynchronouslySetFontName方法,首先先驗(yàn)證是否存在該字體
UIFont* aFont = [UIFont fontWithName:fontName size:12.];// If the font is already downloadedif (aFont && ([aFont.fontName compare:fontName] == NSOrderedSame || [aFont.familyName compare:fontName] == NSOrderedSame)) {// Go ahead and display the sample text.NSUInteger sampleIndex = [_fontNames indexOfObject:fontName];_fTextView.text = [_fontSamples objectAtIndex:sampleIndex];_fTextView.font = [UIFont fontWithName:fontName size:24.];return;}如果不存在改字體,那么aFont將會(huì)返回nil不執(zhí)行該判斷語(yǔ)句,如果存在就直接使用并返回。
接下來看看不存在時(shí),是如何進(jìn)行字體下載的:
1 // Create a dictionary with the font's PostScript name. 2 NSMutableDictionary *attrs = [NSMutableDictionary dictionaryWithObjectsAndKeys:fontName, kCTFontNameAttribute, nil]; 3 4 // Create a new font descriptor reference from the attributes dictionary. 5 CTFontDescriptorRef desc = CTFontDescriptorCreateWithAttributes((__bridge CFDictionaryRef)attrs); 6 7 NSMutableArray *descs = [NSMutableArray arrayWithCapacity:0]; 8 [descs addObject:(__bridge id)desc]; 9 CFRelease(desc); 10 11 __block BOOL errorDuringDownload = NO;首先配置我們需要下載字體的屬性,將fontName作為值kCTFontNameAttribute作為鍵放入字典中。
然后使用CTFontDescriptorCreateWithAttribute來創(chuàng)建一個(gè)字體描述器并將NSDictionary轉(zhuǎn)為CFDictionaryRef作為參數(shù)傳入。
將CTFontDescriptorRef放入數(shù)組中(同樣需要轉(zhuǎn)為對(duì)象)。
接下來需要調(diào)用
CTFontDescriptorMatchFontDescriptorsWithProgressHandler( (__bridge CFArrayRef)descs, NULL,? ^(CTFontDescriptorMatchingState state, CFDictionaryRef progressParameter)
來判斷是否已經(jīng)匹配到了字體,第一個(gè)參數(shù)使我們的描述字體數(shù)組,第二個(gè)設(shè)為NULL,第三個(gè)參數(shù)為回調(diào)block。
block的state參數(shù)為當(dāng)前匹配的狀態(tài),progressParmeter為進(jìn)度參數(shù),其中也包含錯(cuò)誤信息。
我們所用到的state有下面這些:
kCTFontDescriptorMatchingDidBegin? //開始匹配
kCTFontDescriptorMatchingDidFinish //匹配成功
kCTFontDescriptorMatchingWillBeginDownloading//字體開始下載
kCTFontDescriptorMatchingDidFinishDownloading//字體下載成功
kCTFontDescriptorMatchingDownloading //下載中
kCTFontDescriptorMatchingDidFailWithError //匹配失敗
這些狀態(tài)的回調(diào)順序?yàn)?/p>
kCTFontDescriptorMatchingDidBegin
kCTFontDescriptorMatchingWillBeginDownloading
kCTFontDescriptorMatchingDownloading(多次調(diào)用)
kCTFontDescriptorMatchingDidFinishDownloading
kCTFontDescriptorMatchingDidFinish
其中kCTFontDescriptorMatchingDownloading會(huì)多次調(diào)用,來方便我們更新下載的進(jìn)度條。
沒接到一個(gè)回調(diào)狀態(tài)我們就可以進(jìn)行相應(yīng)的UI處理,可以使用block,也可以使用通知。
progressParmeter我們用到了兩個(gè)屬性
因?yàn)樗荂FDictionaryRef,所以首先我們應(yīng)該先把它轉(zhuǎn)為NSDictionary
當(dāng)前下載進(jìn)度的鍵為kCTFontDescriptorMatchingPercentage
錯(cuò)誤對(duì)象的鍵為kCTFontDescriptorMatchingError
如果返回的狀態(tài)是kCTFontDescriptorMatchingDidFailWithError,那我們就可以通過kCTFontDescriptorMatchingError來得到錯(cuò)誤日志了。
轉(zhuǎn)載于:https://www.cnblogs.com/madpanda/p/4279141.html
總結(jié)
- 上一篇: Birdwatching
- 下一篇: 是技术进步,还是心理成熟?