某些字体没有显示出来? [英] Certain fonts not showing up?
问题描述
我在我的应用程序中有一个字体名称列表,它们都以它们所代表的字体显示。以下三种方式不起作用:
ArialRoundedMTBold
ChalkboardSE-Bold
TrebuchetMS-Bold
相反,它们以标准字体显示。
任何想法为什么这些可能不会显示?
如何配置一个自定义字体
要使用一个自定义字体(一个不包含在iOS中)你必须编辑你的< appname> -Info.plist< / code>文件并用类型数组创建一个新的
UIAppFonts
其中数组的每个元素都是一个带有字体文件名称的字符串。例如: VAGRoundedStd-Light.ttf
。您还必须将文件添加到您的项目。
UIAppFonts 并按下回车键时,键被转换成UIAppFonts>
但是,当您在Interface Builder中使用字体(或使用 UIFont
)您不使用字体的文件名,而是在您的Mac的应用程序 Font Book 中打开字体时显示的名称。对于前面的例子,它会是 VAG Rounded Std Light
。
OS X比iOS消化TrueType格式更容忍,所以在极少数情况下,您可能会发现OS X中可以使用的字体,但在iOS中却不行。如果发生这种情况,请更换字体,看看是否至少得到了正确的处理过程。
如何以编程方式加载字体
这解决了字体许可要求您分发加密字体的情况。
- 第一步是使用您认为合适的算法对字体进行加密和解密。
- 将字体加载为NSData对象并反向加密。
- 以程式化方式注册字体。 ://www.marco.org/2012/12/21/ios-dynamic-font-loadingrel =nofollow noreferrer> Marco Arment动态加载iOS字体。它使字体在UIFont和UIWebView中可用。
- 在Fontforge中打开您的字体
- 转到'元素' - >'TTF名称'并更改字段'首选系列
- 转至'文件' - >'生成字体'并保存您的编辑字体
- First step is to encrypt and decrypt the font with whatever algorithm you see fit.
- Load the font as a NSData object and reverse the encryption.
- Register the font programmatically.
- Open your Font in Fontforge
- Goto ‘Element’ -> ‘Font Info’ and change the ‘Family Name’ field
- Goto ‘Element’ -> ‘TTF Names’ and change the fields ‘Family’ and ‘Preferred Family’
- Goto ‘File’ -> ‘Generate Fonts’ and save your edited font
NSData * inData = / *解密后的字体文件数据* /;
CFErrorRef错误;
CGDataProviderRef provider = CGDataProviderCreateWithCFData((CFDataRef)inData);
CGFontRef font = CGFontCreateWithDataProvider(provider);
if(!CTFontManagerRegisterGraphicsFont(font,& error)){
CFStringRef errorDescription = CFErrorCopyDescription(error)
NSLog(@Failed to load font:%@,errorDescription);
CFRelease(errorDescription);
}
CFRelease(font);
CFRelease(提供者);
如何加载两个以上相同系列的字体
在这种情况下,iOS拒绝加载来自同一系列的两种以上的字体。
以下是来自于stackoverflow用户的代码解决方法 Evadne Wu 。只需将以下内容粘贴到您的应用程序委托(请注意,它使用两个框架):
#import< CoreText / CoreText.h> ;
#import< MobileCoreServices / MobileCoreServices.h> $(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
CTFontManagerRegisterFontsForURLs((__ bridge CFArrayRef)((^ {
NSFileManager * fileManager = [NSFileManager defaultManager];
NSURL * resourceURL = [[NSBundle mainBundle] resourceURL];
NSArray * resourceURLs = [fileManager contentsOfDirectoryAtURL:resourceURL includesPropertiesForKeys:nil options:0 error:nil];
return [resourceURLs filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^ BOOL(NSURL * url,NSDictionary * bindings){
CFStringRef pathExtension =(__bridge CFStringRef)[url pathExtension];
NSArray * allIdentifiers =(__bridge_transfer NSArray *)UTTypeCreateAllIdentifiersForTag(kUTTagClassFilenameExtension,pathExtension,CFSTR(public.font));
if(![allIdentifiers count]){
return NO;
}
CFSt ringRef utType =(__bridge CFStringRef)[allIdentifiers lastObject];
return(!CFStringHasPrefix(utType,CFSTR(dyn。))&& UTTypeConformsTo(utType,CFSTR(public.font)));
}]];
))()),kCTFontManagerScopeProcess,nil);
return YES;
可以 gist 。在作者博客中评论:在同一个字体系列的iOS上加载2+字体。
如果添加了两个以上相同字体系列的字体变体(例如
Normal,Bold和Extended),只有您添加到项目中的
的最后两种字体变体才可用。
如果看到这种情况发生,这是SDK版本的限制,唯一的出路就是编辑字体系列用像Fontforge这样的字体编辑器。再次,从 $ b
li>转到'元素' - >'字体信息'并更改'姓氏'字段
I have a list of font names in my app, which are all displayed in the font they represent. The following three do not work though:
ArialRoundedMTBold
ChalkboardSE-Bold
TrebuchetMS-Bold
Instead they display in the standard font.
Any ideas why these might not be showing up?
How to configure a custom font
To use a custom font (one not included in iOS) you have to edit your <appname>-Info.plist
file and create a new UIAppFonts
key with type array, where each element of the array is a String with the name of your font file. Example: VAGRoundedStd-Light.ttf
. You also have to add the file to your project.
Note: When you type UIAppFonts
and press enter, the key is converted to "Fonts provided by application".
However, when you use the font in Interface Builder (or with UIFont
) you don't use the filename of the font, but the name that appears when you open the font in the application Font Book of your Mac. For the previous example it would be VAG Rounded Std Light
.
OS X is more tolerant than iOS digesting TrueType formats, so on rare occasions you may find a font that works in OS X but not in iOS. If that happens, replace the font to see if at least you got the process right.
How to load a font programmatically
This solves the case where the font license requires you to distribute the font encrypted.
This following recipe is from Loading iOS fonts dynamically by Marco Arment. It makes the fonts available in UIFont and UIWebView. The same code can be used to load fonts from the Internet.
NSData *inData = /* your decrypted font-file data */;
CFErrorRef error;
CGDataProviderRef provider = CGDataProviderCreateWithCFData((CFDataRef)inData);
CGFontRef font = CGFontCreateWithDataProvider(provider);
if (! CTFontManagerRegisterGraphicsFont(font, &error)) {
CFStringRef errorDescription = CFErrorCopyDescription(error)
NSLog(@"Failed to load font: %@", errorDescription);
CFRelease(errorDescription);
}
CFRelease(font);
CFRelease(provider);
How to load more than two fonts of the same family
This is the case where iOS refuses to load more than two fonts from the same family.
Here is a code workaround from stackoverflow user Evadne Wu. Simply stick the following in your app delegate (note that it uses two frameworks):
#import <CoreText/CoreText.h>
#import <MobileCoreServices/MobileCoreServices.h>
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
CTFontManagerRegisterFontsForURLs((__bridge CFArrayRef)((^{
NSFileManager *fileManager = [NSFileManager defaultManager];
NSURL *resourceURL = [[NSBundle mainBundle] resourceURL];
NSArray *resourceURLs = [fileManager contentsOfDirectoryAtURL:resourceURL includingPropertiesForKeys:nil options:0 error:nil];
return [resourceURLs filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(NSURL *url, NSDictionary *bindings) {
CFStringRef pathExtension = (__bridge CFStringRef)[url pathExtension];
NSArray *allIdentifiers = (__bridge_transfer NSArray *)UTTypeCreateAllIdentifiersForTag(kUTTagClassFilenameExtension, pathExtension, CFSTR("public.font"));
if (![allIdentifiers count]) {
return NO;
}
CFStringRef utType = (__bridge CFStringRef)[allIdentifiers lastObject];
return (!CFStringHasPrefix(utType, CFSTR("dyn.")) && UTTypeConformsTo(utType, CFSTR("public.font")));
}]];
})()), kCTFontManagerScopeProcess, nil);
return YES;
}
Available as gist. Commented in the author blog: Loading 2+ fonts on iOS from the same font family.
An alternative, more involved workaround from pixeldock.com:
If you add more than 2 font variants of the same font family (e.g. "Normal", "Bold" and "Extended"), only the last two font variants that you added to the project will be usable.
If you see this happening, it is a limitation of your SDK version, and the only way out of it is editing the font family with a Font editor like Fontforge. Again, from pixeldock.com:
这篇关于某些字体没有显示出来?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!