iOS下载使用系统字体
2023-09-11 14:15:06 时间
iOS下载使用系统字体
通用开发中一般使用系统默认的字体;
另外系统也提供了一些其他字体我们可以选择下载使用
1:在mac上打开 字体册 app 即可查找系统支持的字体,适用于ios上开发使用
从mac 字体册上,主要是寻找所要的字体的PostScript 以方便在ios上下载使用
2. 以下代码主要是从苹果官网下载的例子中摘录的,在此标记备忘
如上图:假如我们要下载字体的PostScript 为 :STBaoli-SC-Regular
先来判断该字体是否在系统中存在,如果存在使用,不存在则下载
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
NSString *ffontName = @"STBaoli-SC-Regular"; UIFont* aFont = [UIFont fontWithName:fontName size:12.]; // 判断字体是否已经下载 if (aFont && ([aFont.fontName compare:fontName] == NSOrderedSame || [aFont.familyName compare:fontName] == NSOrderedSame)) { // 使用已下载的字体 NSUInteger sampleIndex = [_fontNames indexOfObject:fontName]; _fTextView.text = [_fontSamples objectAtIndex:sampleIndex]; _fTextView.font = [UIFont fontWithName:fontName size:24.]; return; }
下载指定字体代码
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
// 创建下载字体请求描述的准备 NSMutableDictionary *attrs = [NSMutableDictionary dictionaryWithObjectsAndKeys:fontName, kCTFontNameAttribute, nil]; CTFontDescriptorRef desc = CTFontDescriptorCreateWithAttributes((__bridge CFDictionaryRef)attrs); NSMutableArray *descs = [NSMutableArray arrayWithCapacity:0]; [descs addObject:(__bridge id)desc]; CFRelease(desc); //开始下载字体 __block BOOL errorDuringDownload = NO; CTFontDescriptorMatchFontDescriptorsWithProgressHandler( (__bridge CFArrayRef)descs, NULL, ^(CTFontDescriptorMatchingState state, CFDictionaryRef progressParameter) { //NSLog( @"state %d - %@", state, progressParameter); double progressValue = [[(__bridge NSDictionary *)progressParameter objectForKey:(id)kCTFontDescriptorMatchingPercentage] doubleValue]; if (state == kCTFontDescriptorMatchingDidBegin) { dispatch_async( dispatch_get_main_queue(), ^ { // Show an activity indicator [_fActivityIndicatorView startAnimating]; _fActivityIndicatorView.hidden = NO; // Show something in the text view to indicate that we are downloading _fTextView.text= [NSString stringWithFormat:@"Downloading %@", fontName]; _fTextView.font = [UIFont systemFontOfSize:14.]; NSLog(@"开始匹配..."); }); } else if (state == kCTFontDescriptorMatchingDidFinish) { dispatch_async( dispatch_get_main_queue(), ^ { // Remove the activity indicator [_fActivityIndicatorView stopAnimating]; _fActivityIndicatorView.hidden = YES; // Display the sample text for the newly downloaded font NSUInteger sampleIndex = [_fontNames indexOfObject:fontName]; _fTextView.text = [_fontSamples objectAtIndex:sampleIndex]; _fTextView.font = [UIFont fontWithName:fontName size:24.]; // Log the font URL in the console CTFontRef fontRef = CTFontCreateWithName((__bridge CFStringRef)fontName, 0., NULL); CFStringRef fontURL = CTFontCopyAttribute(fontRef, kCTFontURLAttribute); NSLog(@"%@", (__bridge NSURL*)(fontURL)); CFRelease(fontURL); CFRelease(fontRef); if (!errorDuringDownload) { NSLog(@"%@ downloaded", fontName); } }); } else if (state == kCTFontDescriptorMatchingWillBeginDownloading) { dispatch_async( dispatch_get_main_queue(), ^ { // Show a progress bar _fProgressView.progress = 0.0; _fProgressView.hidden = NO; NSLog(@"开始下载..."); }); } else if (state == kCTFontDescriptorMatchingDidFinishDownloading) { dispatch_async( dispatch_get_main_queue(), ^ { // Remove the progress bar _fProgressView.hidden = YES; NSLog(@"下载完成"); }); } else if (state == kCTFontDescriptorMatchingDownloading) { dispatch_async( dispatch_get_main_queue(), ^ { // Use the progress bar to indicate the progress of the downloading [_fProgressView setProgress:progressValue / 100.0 animated:YES]; NSLog(@"下载进度 %.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(), ^ { _fProgressView.hidden = YES; NSLog(@"下载失败: %@", _errorMessage); }); } return (bool)YES; });
3. 完整示例工程
https://developer.apple.com/library/content/samplecode/DownloadFont/Introduction/Intro.html
相关文章
- 《iOS开发指南:从零基础到App Store上架(第2版)》
- iOS常用数学方法
- 【iOS开发-47】怎样下载iOS 7.1 Simulator 以及iOS 8离线的Documentation这些文件?
- Cocos2d-x--iOS平台lua加密成luac资源方法和Jsc文件<MAC平台开发试用--windows平台暂未研究>
- iOS 11开发教程(十三)iOS11应用编辑界面添加视图
- 《iOS 9应用开发入门经典(第7版)》——第2章,第2.5节问与答
- iOS App被拒原因以及解决方案总结。
- iOS开发UI篇—ios应用数据存储方式(归档)
- iOS SwiftUI 王者荣耀经验学习ScrollView
- iOS应用内抓包、NSURLProtocol 拦截 APP 内的网络请求
- iOS开发中的Markdown渲染
- 详解iOS多图下载的缓存机制
- 如何让Android和iOS应用下载共用一个二维码及遇到的坑
- 在Unity中打开iOS手机上的其他App的方法
- iOS开发 - NSTimer极限使用
- iOS - 国际化语言切换
- iOS开发之网络编程--4、NSURLSessionDataTask实现文件下载(离线断点续传下载) <进度值显示优化>
- iOS开发之网络编程--3、NSURLSessionDataTask实现文件下载(离线断点续传下载)
- iOS开发之网络编程--使用NSURLConnection实现大文件断点续传下载+使用输出流代替文件句柄
- iOS之UI--Quartz2D的入门应用--重绘下载圆形进度条
- 《慕客网:IOS基础入门之Foundation框架初体验》学习笔记 <四> NSMutableArray
- iOS聊天起泡(背景图片被拉伸不变形)----转载--待验证