日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

IOS字体下载

發(fā)布時(shí)間:2024/1/18 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 IOS字体下载 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

結(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é)

以上是生活随笔為你收集整理的IOS字体下载的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。