ios字体动态下载

一、IOS字体

动态下载字体,不仅可以减少APP包的大小,而且字体在iOS系统中是公共的,可共用的。所以如果自己用到的字体已经下载字体,就不用再次下载。还有就是系统提供的字体是iOS维护的。如果用到第三方字体,不仅字体大小对流量、包大小有影响,而且会有版权等的诸多限制。真机下载后的字体路径是在

file:///private/var/MobileAsset/Assets/com_apple_MobileAsset_Font2/25eb7390708d494864eef0905635e1dc3f2e0297.asset/AssetData/Libian.ttc

二、查看需要的字体

可以通过MAC 下字体册查看自己需要的字体

三、动态下载字体代码:

直接上代码(官方demo : https://developer.apple.com/library/ios/samplecode/DownloadFont/Listings/DownloadFont_ViewController_m.html)

- (void)asynchronouslySetFontName:(NSString *)fontName

{

UIFont* aFont = [UIFontfontWithName:fontName
size:12.];

// If the font is already downloaded

if (aFont && ([aFont.fontNamecompare:fontName]
== NSOrderedSame || [aFont.familyNamecompare:fontName] ==
NSOrderedSame)) {

// Go ahead and display the sample text.

NSUInteger sampleIndex = [_fontNamesindexOfObject:fontName];

_fTextView.text = [_fontSamplesobjectAtIndex:sampleIndex];

_fTextView.font = [UIFontfontWithName:fontName
size:24.];

return;

}

// Create a dictionary with the font‘s PostScript name.

NSMutableDictionary *attrs = [NSMutableDictionarydictionaryWithObjectsAndKeys:fontName,kCTFontNameAttribute,
nil];

// Create a new font descriptor reference from the attributes dictionary.

CTFontDescriptorRef desc =CTFontDescriptorCreateWithAttributes((__bridgeCFDictionaryRef)attrs);

NSMutableArray *descs = [NSMutableArrayarrayWithCapacity:0];

[descs addObject:(__bridgeid)desc];

CFRelease(desc);

__blockBOOL 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( (__bridgeCFArrayRef)descs,NULL, 
^(CTFontDescriptorMatchingState state,CFDictionaryRef progressParameter) {

//NSLog( @"state %d - %@", state, progressParameter);

double progressValue = [[(__bridgeNSDictionary
*)progressParameterobjectForKey:(id)kCTFontDescriptorMatchingPercentage]doubleValue];

if (state ==kCTFontDescriptorMatchingDidBegin) {

dispatch_async(dispatch_get_main_queue(), ^ {

// Show an activity indicator

[_fActivityIndicatorViewstartAnimating];

_fActivityIndicatorView.hidden =NO;

// Show something in the text view to indicate that we are downloading

_fTextView.text= [NSStringstringWithFormat:@"Downloading
%@", fontName];

_fTextView.font = [UIFontsystemFontOfSize:14.];

NSLog(@"Begin Matching");

});

} elseif (state ==kCTFontDescriptorMatchingDidFinish)
{

dispatch_async(dispatch_get_main_queue(), ^ {

// Remove the activity indicator

[_fActivityIndicatorViewstopAnimating];

_fActivityIndicatorView.hidden =YES;

// Display the sample text for the newly downloaded font

NSUInteger sampleIndex = [_fontNamesindexOfObject:fontName];

_fTextView.text = [_fontSamplesobjectAtIndex:sampleIndex];

_fTextView.font = [UIFontfontWithName:fontName
size:24.];

// Log the font URL in the console

CTFontRef fontRef =CTFontCreateWithName((__bridgeCFStringRef)fontName,
0., NULL);

CFStringRef fontURL =CTFontCopyAttribute(fontRef,kCTFontURLAttribute);

NSLog(@"%@", (__bridgeNSString*)(fontURL));

CFRelease(fontURL);

CFRelease(fontRef);

if (!errorDuringDownload) {

NSLog(@"%@ downloaded", fontName);

}

});

} elseif (state ==kCTFontDescriptorMatchingWillBeginDownloading)
{

dispatch_async(dispatch_get_main_queue(), ^ {

// Show a progress bar

_fProgressView.progress =0.0;

_fProgressView.hidden =NO;

NSLog(@"Begin Downloading");

});

} elseif (state ==kCTFontDescriptorMatchingDidFinishDownloading)
{

dispatch_async(dispatch_get_main_queue(), ^ {

// Remove the progress bar

_fProgressView.hidden =YES;

NSLog(@"Finish downloading");

});

} elseif (state ==kCTFontDescriptorMatchingDownloading)
{

dispatch_async(dispatch_get_main_queue(), ^ {

// Use the progress bar to indicate the progress of the downloading

[_fProgressViewsetProgress:progressValue /100.0
animated:YES];

NSLog(@"Downloading %.0f%% complete", progressValue);

});

} elseif (state ==kCTFontDescriptorMatchingDidFailWithError)
{

// An error has occurred.

// Get the error message

NSError *error = [(__bridgeNSDictionary *)progressParameterobjectForKey:(id)kCTFontDescriptorMatchingError];

if (error !=nil) {

_errorMessage = [errordescription];

} else {

_errorMessage =@"ERROR MESSAGE IS NOT AVAILABLE!";

}

// Set our flag

errorDuringDownload = YES;

dispatch_async(dispatch_get_main_queue(), ^ {

_fProgressView.hidden =YES;

NSLog(@"Download error: %@",_errorMessage);

});

}

return (bool)YES;

});

}

字体下载状态:

typedef CF_ENUM(uint32_t, CTFontDescriptorMatchingState) {

kCTFontDescriptorMatchingDidBegin,              // called once at the beginning.

kCTFontDescriptorMatchingDidFinish,             // called once at the end.

kCTFontDescriptorMatchingWillBeginQuerying,    
// called once before talking to the server.  Skipped if not necessary.

kCTFontDescriptorMatchingStalled,              
// called when stalled. (e.g. while waiting for server response.)

// Downloading and activating are repeated for each descriptor.

kCTFontDescriptorMatchingWillBeginDownloading, 
// Downloading part may be skipped if all the assets are already downloaded

kCTFontDescriptorMatchingDownloading,

kCTFontDescriptorMatchingDidFinishDownloading,

kCTFontDescriptorMatchingDidMatch,              // called when font descriptor is matched.

kCTFontDescriptorMatchingDidFailWithError      
// called when an error occurred.  (may be called multiple times.)

};

1、准备开始下载

kCTFontDescriptorMatchingDidBegin

2、开始匹配对应字体

Begin Matching

Printing description of progressParameter:

{

CTFontDescriptorMatchingCurrentAssetSize = 11576071;

CTFontDescriptorMatchingDescriptors =     (

"UICTFontDescriptor <0x1278a0b40> = {\n    NSFontNameAttribute = \"DFWaWaSC-W5\";\n}"

);

CTFontDescriptorMatchingTotalAssetSize = 11576071;

CTFontDescriptorMatchingTotalDownloadedSize = 0;

}

3、找到对应字体

state = kCTFontDescriptorMatchingDidMatch

Printing description of progressParameter:

{

CTFontDescriptorMatchingDescriptors =     (

"UICTFontDescriptor <0x148105bb0> = {\n    NSFontNameAttribute = \"STLibian-SC-Regular\";\n}"

);

CTFontDescriptorMatchingResult =     (

"UICTFontDescriptor <0x148128ee0> = {\n    NSFontNameAttribute = \"STLibian-SC-Regular\";\n}"

);

CTFontDescriptorMatchingSourceDescriptor = "UICTFontDescriptor <0x148105bb0> = {\n    NSFontNameAttribute = \"STLibian-SC-Regular\";\n}";

CTFontDescriptorMatchingTotalAssetSize = 0;

}

4、开始下载

Downloading 0% complete

Printing description of progressParameter:

{

CTFontDescriptorMatchingCurrentAssetSize = 11576071;

CTFontDescriptorMatchingDescriptors =     (

"UICTFontDescriptor <0x1278a0b40> = {\n    NSFontNameAttribute = \"DFWaWaSC-W5\";\n}"

);

CTFontDescriptorMatchingPercentage = 1;//下载进度

CTFontDescriptorMatchingTotalAssetSize = 11576071;//字体大小

CTFontDescriptorMatchingTotalDownloadedSize = 262811;//已下载大小

}

5、下载完成

kCTFontDescriptorMatchingDidFinish

Printing description of progressParameter:

{

CTFontDescriptorMatchingDescriptors =     (

"UICTFontDescriptor <0x146da8850> = {\n    NSFontNameAttribute = \"STXingkai-SC-Light\";\n}"

);

CTFontDescriptorMatchingResult =     (

"UICTFontDescriptor <0x1480caee0> = {\n    NSFontNameAttribute = \"STXingkai-SC-Light\";\n}"

);

CTFontDescriptorMatchingTotalAssetSize = 0;

}

时间: 2024-10-10 23:25:49

ios字体动态下载的相关文章

iOS 字体下载

iOS可以动态的为系统下载字体,这些字体都下载到了系统的目录下,并且可以被其他应用公用 来看下如何实现动态下载: // 创建下载字体请求描述的准备 NSMutableDictionary *attrs = [NSMutableDictionary dictionaryWithObjectsAndKeys:fontName, kCTFontNameAttribute, nil]; CTFontDescriptorRef desc = CTFontDescriptorCreateWithAttrib

使用CoreText动态下载更换字体

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #d12f1b } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #000000; min-height: 16.0px } p.p3 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #703daa }

IOS字体下载

结合书本与苹果官方给的例子后,总结下下载的方法. 苹果给我们提供了很多漂亮的字体,只是有些字体设备并没有内置,需要我们去下载才行. 系统提供给我们的字体名我们可以通过mac系统提供的字体册来查阅. 得到我们想要的字体后就可以在我们的设备上进行下载了.这里要说一下,设备字体下载后是所有应用都可以使用的,而且字体的目录并不是我们APP的目录,因此并不会增大我们应用所需的空间. 这里结合着苹果官方所给例子来简述一下(官方例子): 事例中给我们预定了几种字体来让我们下载 1 - (void)viewDi

一文让你彻底了解iOS字体相关知识

作者:董铂然 授权本站转载. 写本文的契机主要是把自己整理的关于iOS字体方面的知识不断更新写在这篇博文中,用来自己以后查阅. 一.iOS原生字体展示 在 label中选择字体的font,并把font由system改成custom后,就能在family中看到72种特殊字体.这些里面就有很炫的字体,但 是全部是只针对英文数字,对中文无效.写了一个程序把所有的原生样式遍历出来展示可以达到如下效果.可以清楚地看到每个字体对应的样式,不用再一个个试 了. 如果你不是在董铂然博客园看到本文,请点击查看原文

iOS本地动态验证码生成-b

用于ios本地动态生成验证码,效果如下: demo.gif 导入CoreGraphics.framework用于绘制图形 封装UIView,便捷使用,代码如下: AuthcodeView.h #import <UIKit/UIKit.h> @interface AuthcodeView : UIView @property (strong, nonatomic) NSArray *dataArray;//字符素材数组 @property (strong, nonatomic) NSMutabl

WWDC2014之iOS使用动态库 framework【转】

from:http://www.cocoachina.com/industry/20140613/8810.html JUN 12TH, 2014 苹果的开放态度 WWDC2014上发布的Xcode6 beta版有了不少更新,其中令我惊讶的一个是苹果在iOS上开放了动态库,在Xcode6 Beta版的更新文档中是这样描述的: Frameworks for iOS. iOS developers can now create dynamic frameworks. Frameworks are a

WWDC2014之iOS使用动态库

苹果的开放态度 WWDC2014上发布的Xcode6 beta版有了不少更新,其中令我惊讶的一个是苹果在iOS上开放了动态库,在Xcode6 Beta版的更新文档中是这样描述的: Frameworks for iOS. iOS developers can now create dynamic frameworks. Frameworks are a collection of code and resources to encapsulate functionality that is val

iOS 字体详解

一.iOS原生字体获取及展示 1.xib/storyboard 图形展示 拖拽创建一个Label控件,选中该Label,在设置中把Label字体System修改为自定义(custom),然后点击family选框,可以查看到所有的原生字体. 2.代码获取字体及设置 很多时候我们是用纯代码进行编程,这时我们该如何设置文本字体呢? 其实我们可以用两个for循环取出所有的字体名称,然后根据自己的需求去设置字体. 获取所有字体名称代码: 1 - (void)getAllFont{ 2 for (NSStr

推荐一种适合程序员的字体(附下载地址及各平台安装方法)

经常写代码需要找一种看起来舒服的字体,她至少要满足: 字母和数字易于分辨,如: 0和o, 1和l, '' 和 " (两个单引号和双引号) 字体等宽,保持对齐 漂亮 免费 经过测试,推荐以下这款字体 Adobe Source Code Pro 主页: http://adobe-fonts.github.io/source-code-pro/ 效果图: 安装方法: Mac OS 双击已下载的字体文件,点击字体预览下方的"安装字体"按钮,即可 在 Finder 中选取“前往”>