iOS UIFont 的學習與使用
通常,我們使用字體 都是系統默認的字體. 有時候 從閱讀體驗,美觀度 設計師都會考慮用一些 更高大尚的字體. 系統字體庫 給英文 各種style的發揮空間很大,但是 中文則不然.
但是蘋果 給使用中文的字體的開發者提供了 動態下載字體庫的福利,這個真是好,并且下載到/private/var/mobile/Library/Assets/com_apple_MobileAsset_Font/ 這樣不會增加app本身的大小. 不失為一種好的選擇.
前提: 你首先 要知道 你要使用的字體的官方名字, 用這個名字 當索引下載.
官方竟然有文檔和demo 真是方便的不要不要的:
https://developer.apple.com/library/ios/samplecode/DownloadFont/Listings/DownloadFont_ViewController_m.html
操作基本步驟
1. 首先判斷 是否有這種字體 無則用默認的
(如果 要使用一些 iOS8,iOS9才有的字體 一定要考慮低版本用戶的情況,會閃退,在 font 類別里面也要做預判斷,沒有這個字體 就用默認的唄)
+ (UIFont *)normalHfFontWithSize: (CGFloat)fontSize { return [UIFont isFontDownloaded:@"FZLTXHK--GBK1-0"] ? [UIFont fontWithName:@"FZLTXHK--GBK1-0" size:fontSize] : [UIFont systemFontWithSize:fontSize]; } + (BOOL)isFontDownloaded:(NSString *)fontName { UIFont* aFont = [UIFont fontWithName:fontName size:12.0]; if (aFont && ([aFont.fontName compare:fontName] == NSOrderedSame || [aFont.familyName compare:fontName] == NSOrderedSame)) { return YES; } else { return NO; } }
2. 我的下載策略 是 打開應用 就在父線程里面 處理 需要的字體下載等事宜 , 也有人 是用再下載 ,看需求吧
Reachability *r = [Reachability reachabilityForInternetConnection]; [r startNotifier]; NetworkStatus netStatus = [r currentReachabilityStatus]; NSArray *fonts = @[@"FZLTZHK--GBK1-0",@"FZLTXHK--GBK1-0"]; for (NSString *font in fonts) { NSString *isFontAvailable = [[NSUserDefaults standardUserDefaults] objectForKey:[NSString stringWithFormat:@"FontDownloaded%@",font]]; if ((netStatus == ReachableViaWiFi && ![isFontAvailable isEqualToString:font]) || [isFontAvailable isEqualToString:font]) { [UIFont downloadFont:font]; } }
+ (void)downloadFont:(NSString *)fontName { UIFont* aFont = [UIFont fontWithName:fontName size:12.]; // If the font is already downloaded if (aFont && ([aFont.fontName compare:fontName] == NSOrderedSame || [aFont.familyName compare:fontName] == NSOrderedSame)) { // Go ahead and display the sample text. 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中NSMutableArray *descs = [NSMutableArray arrayWithCapacity:0]; [descs addObject:(__bridge id)desc]; CFRelease(desc); __block BOOL errorDuringDownload = NO; // Start processing the font descriptor.. // This function returns immediately, but can potentially take long time to process. // The progress is notified via the callback block of CTFontDescriptorProgressHandler type. // See CTFontDescriptor.h for the list of progress states and keys for progressParameter dictionary. CTFontDescriptorMatchFontDescriptorsWithProgressHandler( (__bridge CFArrayRef)descs, NULL, ^(CTFontDescriptorMatchingState state, CFDictionaryRef progressParameter) { NSString *errorMessage; DLog( @"state %d - %@", state, progressParameter); // double progressValue = [[(__bridge NSDictionary *)progressParameter objectForKey:(id)kCTFontDescriptorMatchingPercentage] doubleValue]; if (state == kCTFontDescriptorMatchingDidBegin) { dispatch_async( dispatch_get_main_queue(), ^ { DLog(@"Begin Matching"); }); } 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); DLog(@"%@", (__bridge NSURL*)(fontURL)); CFRelease(fontURL); CFRelease(fontRef); if (!errorDuringDownload) { [[NSUserDefaults standardUserDefaults] setObject:fontName forKey:[NSString stringWithFormat:@"FontDownloaded%@",fontName]]; [[NSUserDefaults standardUserDefaults] synchronize]; DLog(@"%@ downloaded", fontName); } }); } else if (state == kCTFontDescriptorMatchingWillBeginDownloading) { dispatch_async( dispatch_get_main_queue(), ^ { DLog(@"Begin Downloading"); }); } else if (state == kCTFontDescriptorMatchingDidFinishDownloading) { dispatch_async( dispatch_get_main_queue(), ^ { DLog(@"Finish downloading");// 可以在這里修改UI控件的字體}); } else if (state == kCTFontDescriptorMatchingDownloading) { dispatch_async( dispatch_get_main_queue(), ^ { //DLog(@"Downloading %.0f%% complete", progressValue); }); } else if (state == kCTFontDescriptorMatchingDidFailWithError) { // An error has occurred. // Get the error message NSError *error = [(__bridge NSDictionary *)progressParameter objectForKey:(id)kCTFontDescriptorMatchingError]; if (error != nil) { errorMessage = [error description]; } else { errorMessage = @"ERROR MESSAGE IS NOT AVAILABLE!"; } // Set our flag設置標志errorDuringDownload = YES; dispatch_async( dispatch_get_main_queue(), ^ { DLog(@"Download error: %@", errorMessage); }); } return (bool)YES; }); }
妥妥的 滿足使用動態下載的字體需求
備注:
其他方法還有 使用字體資源文件(尾綴為.ttf或otf格式文件),上網上搜吧 講解 一堆 .
但是這里存在的問題 1 需要把這個 .ttf文件 target到工程 不像 動態下載不增加 應用本身的大小 的特點
其次是 如果沒有處理版權問題,這個就大發了 是吧...
看需求 正確使用 恰當的方法解決問題吧
參考:
http://www.awnlab.com/archives/2658.html
posted on 2016-06-13 18:58 ACM_Someone like you 閱讀(2309) 評論(0) 收藏 舉報
浙公網安備 33010602011771號