1、最基本的就是遍历手机上目前有的字体,但是这些字体基本都是英文的。
2、第二种就是把ttf文件放到项目里面,如果你查网络上的文章,你会发现要修改info.plist,添加Fonts provided by application。然后把item添加上。
但是我在遍历系统支持的字体,愣是没有?这个坑是怎么回事?
其实需要修改Fonts provided by application 成UIAppFonts。就OK了。浪费了几个小时!(当然你要注意build phase里面的copy bundle source要有字体)
3、第三种就是下载苹果支持的中文字体。
一般都是用下面的方法:
-(BOOL)isFontDownloaded:(NSString *)fontName {
UIFont* aFont = [UIFont fontWithName:fontName size:20.0];
if (aFont && ([aFont.fontName compare:fontName] == NSOrderedSame
|| [aFont.familyName compare:fontName] == NSOrderedSame)) {
return YES;
} else {
return NO;
}
}
-(void)downloadFontWithPostScriptName:(NSString *)postScriptName{
if ([self isFontDownloaded:postScriptName]) {
return;
}
NSMutableDictionary *attrsDictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:postScriptName,kCTFontNameAttribute, nil];
CTFontDescriptorRef descriptor = CTFontDescriptorCreateWithAttributes((__bridge CFDictionaryRef)attrsDictionary);
NSMutableArray *descriptorArray = [NSMutableArray array];
[descriptorArray addObject:(__bridge id)descriptor];
CFRelease(descriptor);
__block BOOL errorDuringDownload = NO;
CTFontDescriptorMatchFontDescriptorsWithProgressHandler((__bridge CFArrayRef)descriptorArray, NULL, ^bool(CTFontDescriptorMatchingState state, CFDictionaryRef _Nonnull progressParameter) {
double progressValue = [[(__bridge NSDictionary *)progressParameter objectForKey:(id)kCTFontDescriptorMatchingPercentage] doubleValue];
if (state == kCTFontDescriptorMatchingDidBegin) {
NSLog(@"字体已经匹配");
}else if (state == kCTFontDescriptorMatchingDidFinish){
if (!errorDuringDownload) {
NSLog(@"字体%@下载完成",postScriptName);
dispatch_async(dispatch_get_main_queue(), ^{
// 在此修改UI控件的字体
testInfo.font=[UIFont fontWithName:@"DFWaWaSC-W5" size:20.0];
[testInfo setText:@"测试一下而已"];
});
}
}else if (state == kCTFontDescriptorMatchingWillBeginDownloading){
NSLog(@"字体开始下载");
}else if (state == kCTFontDescriptorMatchingDidFinishDownloading){
NSLog(@"字体下载完成");
}else if (state == kCTFontDescriptorMatchingDownloading){
NSLog(@"下载进度%.0f%%",progressValue);
}else if (state == kCTFontDescriptorMatchingDidFailWithError){
NSError *error = [(__bridge NSDictionary *)progressParameter objectForKey:(id)kCTFontDescriptorMatchingError];
NSString *errorMesage = [NSString string];
if (error != nil) {
errorMesage = [error description];
} else {
errorMesage = @"ERROR MESSAGE IS NOT AVAILABLE!";
}
// 设置下载失败标志
errorDuringDownload = YES;
NSLog(@"下载错误:%@",errorMesage);
}
return (bool)YES;
});
}
坑来了!
第一次下载字体,设置是有效果的,如果你关闭了ViewController,从新进来,大概要跑这样的代码:
if([self isFontDownloaded:@"DFWaWaSC-W5"])
{
testInfo.font=[UIFont fontWithName:@"DFWaWaSC-W5" size:20.0];
[testInfo setText:@"我是王"];
}else
{
NSLog(@"==========no==yes==");
[self downloadFontWithPostScriptName:@"DFWaWaSC-W5"];
}
如果已经有了,我们直接使用就应该可以了!我是这么想的。直接在viewLoaded就这么用了。抱歉!这么玩“我是王”显示是显示了,但是没有效果。
然后你打印这个font,没毛病,有内容!坑啊~~~~
怎么办呢?其实要这么搞,才出效果,代码如下:
if([self isFontDownloaded:@"DFWaWaSC-W5"])
{
dispatch_async(dispatch_get_main_queue(), ^{
// 在此修改UI控件的字体
testInfo.font=[UIFont fontWithName:@"DFWaWaSC-W5" size:20.0];
[testInfo setText:@"我是王"];
});
}else
{
NSLog(@"==========no==yes==");
[self downloadFontWithPostScriptName:@"DFWaWaSC-W5"];
}