某些字体不显示? [英] Certain fonts not showing up?

查看:36
本文介绍了某些字体不显示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用中有一个字体名称列表,它们都以它们所代表的字体显示.但以下三个不起作用:

ArialRoundedMTBold
黑板SE-粗体
TrebuchetMS-Bold

相反,它们以标准字体显示.

有什么想法为什么这些可能不会出现吗?

解决方案

如何配置自定义字体

要使用自定义字体(iOS 中未包含的字体),您必须编辑 由 Marco Arment 提供.它使 UIFont 和 UIWebView 中的字体可用.相同的代码可用于从 Internet 加载字体.

NSData *inData =/* 你解密的字体文件数据 */;CFErrorRef 错误;CGDataProviderRef provider = CGDataProviderCreateWithCFData((CFDataRef)inData);CGFontRef 字体 = CGFontCreateWithDataProvider(provider);if (!CTFontManagerRegisterGraphicsFont(font, &error)) {CFStringRef errorDescription = CFErrorCopyDescription(error)NSLog(@"加载字体失败:%@", errorDescription);CFRelease(errorDescription);}CFRelease(字体);CFRelease(提供者);

如何加载同一家族的两种以上字体

这是 iOS 拒绝加载来自同一系列的两种以上字体的情况.

这是来自 stackoverflow 用户 Evadne Wu 的代码解决方法.只需在您的应用委托中粘贴以下内容(请注意,它使用两个框架):

#import #import - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{CTFontManagerRegisterFontsForURLs((__bridge CFArrayRef)((^{NSFileManager *fileManager = [NSFileManager defaultManager];NSURL *resourceURL = [[NSBundle mainBundle] resourceURL];NSArray *resourceURLs = [fileManager contentsOfDirectoryAtURL:resourceURL 包括PropertiesForKeys:nil options:0 error:nil];返回 [resourceURLsfilteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(NSURL *url, NSDictionary *bindings) {CFStringRef pathExtension = (__bridge CFStringRef)[url pathExtension];NSArray *allIdentifiers = (__bridge_transfer NSArray *)UTTypeCreateAllIdentifiersForTag(kUTTagClassFilenameExtension, pathExtension, CFSTR("public.font"));如果 (![allIdentifiers 计数]) {返回否;}CFStringRef utType = (__bridge CFStringRef)[allIdentifiers lastObject];return (!CFStringHasPrefix(utType, CFSTR("dyn.")) && UTTypeConformsTo(utType, CFSTR("public.font")));}]];})()), kCTFontManagerScopeProcess, nil);返回是;}

可用作 gist.在作者博客评论:从相同的字体系列在 iOS 上加载 2+ 种字体.

<小时>

来自 pixeldock.com:

<块引用>

如果您添加了相同字体系列的 2 种以上字体变体(例如Normal"、Bold"和Extended"),只有最后两种字体变体您添加到项目中将可用.

如果您看到这种情况发生,则说明您的 SDK 版本存在限制,唯一的解决方法是使用 Fontforge 等字体编辑器编辑字体系列.同样,来自 pixeldock.com:

  1. 在 Fontforge 中打开您的字体
  2. 转到元素"->字体信息"并更改姓氏"字段
  3. 转到元素"->TTF 名称"并更改字段家庭"和首选家庭"
  4. 转到文件"->生成字体"并保存您编辑的字体

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.

  1. First step is to encrypt and decrypt the font with whatever algorithm you see fit.
  2. Load the font as a NSData object and reverse the encryption.
  3. Register the font programmatically.

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:

  1. Open your Font in Fontforge
  2. Goto ‘Element’ -> ‘Font Info’ and change the ‘Family Name’ field
  3. Goto ‘Element’ -> ‘TTF Names’ and change the fields ‘Family’ and ‘Preferred Family’
  4. Goto ‘File’ -> ‘Generate Fonts’ and save your edited font

这篇关于某些字体不显示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆