通常,我们使用字体 都是系统默认的字体. 有时候 从阅读体验,美观度 设计师都会考虑用一些 更高大尚的字体. 系统字体库 给英文 各种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 的学习与使用相关推荐

  1. IOS应用管理学习,进阶,涉及字典转模型,工厂方法,面向对象思想,页面布局等

    IOS应用管理学习,进阶,涉及字典转模型,工厂方法,面向对象思想,页面布局等 前言:人为规定的参数 每一个 小view视图 宽度 80 高度 90 数据类型 CGFloat 定义, 3 列,数据类型 ...

  2. IOS UI Automation 学习之常用类,方法和模拟手势

    为什么80%的码农都做不了架构师?>>>    IOS UI Automation 学习之常用类,方法和模拟手势 常用类结构图 作者不擅长作画,如果有好的画此类图形的工具,可以留言, ...

  3. iOS UIFont 字体名字大全

    iOS UIFont 字体名字大全 Font Family: American Typewriter Font: AmericanTypewriter Font: AmericanTypewriter ...

  4. 0811 iOS开发完整学习路线

    1.iOS开发需要学习哪些内容? 2.开发步骤 3.框架 为了方便开发者开发出强大的功能,苹果提供了各种各样的框架 [1]UIKit:创建和管理应用程序的用户界面 [2]QuartzCore:提供动画 ...

  5. iOS App Clips学习笔记

    一.什么是App Clip? App Clip是一个App的轻量版,用来提供一些用户所需功能,我们可以把它看成苹果的小程序.它不需要下载就直接能打开.这样即可达到不下载完整版APP便可体验APP的部分 ...

  6. 如何 给 iOS UIFont 设置字重?

    如何 给 iOS UIFont 设置字重 UILabel *label = [[UILabel alloc] init]; if (@available(iOS 8.2, *)) {label.fon ...

  7. iOS UIFont自定义字体

    1. 将字体文件导入工程(.ttf) 2. 打开Build Phases -> Copy Bundle Resources,确定字体文件已经添加 3. 编辑info.plist文件,添加Font ...

  8. IOS控件学习:UILabel常用属性与用法

    #import "ViewController.h" #import <CoreText/CoreText.h>@interface ViewController () ...

  9. IOS控件学习:UILabel常用属性与用法(转)

    原文链接:http://duchengjiu.iteye.com/blog/2041391 保存下来随时查看,感谢分享. 参考网站: http://shijue.me/show_text/521c39 ...

最新文章

  1. 一个智能机器人的语录
  2. 网站被k不要慌,看看“老油条”们是怎么解决的吧!
  3. 细胞培养中出现黑胶虫污染处理方法
  4. Request header field Content-Type is not allowed by Access-Control-Allow-Headers跨域
  5. 手把手教你从零构建属于自己的小linux
  6. 在Mono 2.8上部署ASP.NET MVC 2
  7. OpenCV文档阅读笔记-brief Creates a window官方解析及实例
  8. 基于JAVA+SpringBoot+Vue+Mybatis+MYSQL的图书馆管理系统
  9. 使用Xamarin在Visual Studio中开发Android应用
  10. h3cIP和TCP抓包分析实验
  11. 麻省理工18年春软件构造课程阅读04“代码评审”
  12. 关于使用idea输入中文时,候选框不出现在光标附近的问题
  13. 在电脑上下载哔哩哔哩中的视频
  14. 大数据开发治理平台 DataWorks
  15. 深入理解什么是LSM-Tree
  16. Python官方编译器的安装
  17. 1355 斐波那契的最小公倍数
  18. 诗歌(11)—东栏梨花
  19. 【转载】西南大旱,最缺的是水吗?
  20. Qt之创建桌面和开始菜单快捷方式

热门文章

  1. Internet及其信息资源服务
  2. root锤子坚果手机系统,坚果手机root教程
  3. .net中mvc问卷制作
  4. mysql 不小心删除_mysql 不小心删除数据库怎么办
  5. android rootfs根文件系统挂载
  6. centos7升级到centos8
  7. 常用的第三方框架汇总
  8. drupal心得:难的价值、未来趋势、拥抱王者
  9. 什么样的经历,才能领悟成为架构师?
  10. iOS图像处理之画圆角矩形