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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

iOS UIFont 的学习与使用

發布時間:2024/1/18 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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 downloadedif (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 consoleCTFontRef 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 messageNSError *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

轉載于:https://www.cnblogs.com/someonelikeyou/p/5581677.html

總結

以上是生活随笔為你收集整理的iOS UIFont 的学习与使用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。