博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS 字体下载
阅读量:4575 次
发布时间:2019-06-08

本文共 4821 字,大约阅读时间需要 16 分钟。

iOS可以动态的为系统下载字体,这些字体都下载到了系统的目录下,并且可以被其他应用公用

来看下如何实现动态下载:

// 创建下载字体请求描述的准备    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;    });

 

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://www.cnblogs.com/hualuoshuijia/p/10654103.html

你可能感兴趣的文章
python 元组 【基本使用功能】
查看>>
ecplise 使用快捷键
查看>>
微信emoji表情编码 、MySQL 存储 emoji 表情符号字符集
查看>>
Netty心跳之IdleStateHandler
查看>>
hash小结
查看>>
动态调用WebService
查看>>
python练习_module01-2-购物车
查看>>
vue-cli+ webpack 搭建项目todolist
查看>>
[BZOJ 4325][NOIP 2015] 斗地主
查看>>
《Windows核心编程系列》十一谈谈Windows线程池
查看>>
IOS-添加分段控件SegmentControl
查看>>
Flask 基础
查看>>
最长上升子序列(DP+模板)
查看>>
福大软工1816 · 第五次作业 - 结对作业2
查看>>
[LeetCode] 65. Valid Number(多个标志位)
查看>>
●BZOJ 4665 小w的喜糖
查看>>
nginx配置location总结及rewrite规则写法【转】
查看>>
DES加密的C语言实现
查看>>
Adobe Dreamweaver CS5.5 中文版 下载 注册码
查看>>
C#中的静态常量(const)和动态常量(static和readonly)用法和区别
查看>>