某些字体不显示?

发布于 2024-11-06 18:02:25 字数 159 浏览 0 评论 0原文

我的应用程序中有一个字体名称列表,它们全部以它们代表的字体显示。但以下三个不起作用:

ArialRoundedMTBold
黑板SE-粗体
TrebuchetMS-Bold

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

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

晨光如昨 2024-11-13 18:02:25

如何配置自定义字体

要使用自定义字体(iOS 中未包含的字体),您必须编辑 -Info.plist 文件并创建一个新的 UIAppFonts 类型为数组的键,其中数组的每个元素都是带有字体文件名称的字符串。示例:VAGRoundedStd-Light.ttf。您还必须将该文件添加到您的项目中。

UIAppFonts

注意:当您键入 UIAppFonts 并按 Enter 键时,按键将转换为“应用程序提供的字体”。

但是,当您在 Interface Builder 中(或使用 UIFont)使用该字体时,您不会使用该字体的文件名,而是使用该字体时出现的名称。您在 Mac 的应用程序Font Book中打开该字体。对于前面的示例,它将是 VAG Rounded Std Light

Font Book

OS X 比 iOS 更能容忍 TrueType 格式,因此在极少数情况下,您可能会找到适用于 OS 的字体X 但 iOS 中没有。如果发生这种情况,请更换字体,看看至少您的过程是否正确。

如何以编程方式加载字体

这解决了字体许可证要求您分发加密字体的情况。

  1. 第一步是使用您认为合适的任何算法加密和解密字体。
  2. 将字体加载为 NSData 对象并反转加密。
  3. 以编程方式注册字体。

以下秘籍来自 动态加载 iOS 字体马可·阿门特.它使字体在 UIFont 和 UIWebView 中可用。相同的代码可用于从 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);

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

这是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 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;
}

作为 gist 提供。在作者博客中评论: 在 iOS 上加载来自同一字体系列的 2 种以上字体


另一种更复杂的解决方法来自 pixeldock.com

如果您添加同一字体系列的 2 个以上字体变体(例如
“正常”、“粗体”和“扩展”),只有最后两种字体变体
你添加到项目中就可以使用了。

如果您看到这种情况发生,这是您的 SDK 版本的限制,唯一的解决方法是使用 Fontforge 等字体编辑器编辑字体系列。再次,来自 pixeldock.com

  1. 在 Fontforge 中打开你的字体,
  2. 转到“Element”->; “字体信息”并更改“系列名称”字段
  3. 转到“元素”-> “TTF 名称”并更改“家庭”和“首选家庭”字段
  4. 转到“文件”-> “生成字体”并保存编辑的字体

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.

UIAppFonts

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.

Font Book

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
梦过后 2024-11-13 18:02:25

iOS 中仅提供 Mac OS 字体的子集。如果您设置的字体不可用,它将显示为标准 Helvetica。

Only a subset of Mac OS fonts are available in iOS. If you set a font that is not available, it will be displayed as the standard Helvetica.

静水深流 2024-11-13 18:02:25

我使用的是名为 Arvo 的 Google 字体。它具有以下文件:

Arvo-Regular.ttf
Arvo-Bold.ttf
Arvo-BoldItalic.ttf
Arvo-斜体.ttf
这些名称已添加到我的应用程序的 info.plist 中,但由于某种原因,我的应用程序只能读取粗体、粗体和斜体字体。当我尝试加载它时,它无法读取常规字体。然而,打印出字体名称后,Arvo-Regular 在我的应用程序中被识别为 Arvo。

I was using a Google font named Arvo. It had the following files:

Arvo-Regular.ttf
Arvo-Bold.ttf
Arvo-BoldItalic.ttf
Arvo-Italic.ttf
These names were added into my app's info.plist but for some reason, my app could only read the bold, bolditalic, and italic fonts. It couldn't read the regular font when i tried to load it. However, after printing out the font names, it came out that Arvo-Regular was recognized as Arvo in my app.

只是一片海 2024-11-13 18:02:25

我也遇到了类似的问题,找不到 Chalkduster - 尽管 IB 将其列为字体并且将其列为 IOS 字体。我发现 iPhone 和 iPad 上的字体列表并不相同。

Chalkduster 适用于 iPad,但不适用于 iPhone。黑板在 iPad 4.x 上可用,但在 3.x 上不可用。它在 iPhone 3.x 和 iPhone 3.x 上使用。 4.x。

更多详细信息请参见:http:// www.whatsyourdigitaliq.com/2011/05/11/fonts-available-in-various-versions-of-ios/

I had a similar issue with not finding Chalkduster - even though IB listed it as a font and it's listed as an IOS font. I discovered that the list of fonts on iPhone and iPad is not identical.

Chalkduster is available for iPad but not iPhone. Chalkboard is available on iPad 4.x but not 3.x. It is available on iPhone 3.x & 4.x.

More details here: http://www.whatsyourdigitaliq.com/2011/05/11/fonts-available-in-various-versions-of-ios/

玩世 2024-11-13 18:02:25

打开字体簿应用程序。如果您自己安装了字体,请转到用户,查找您想要的字体并在您的 xcode 项目中使用该字体的 PostScript 名称。

它甚至应该适用于同一家族的不同字体变体。

Open Font Book application. If you installed the fonts yourself, go to user, look for the fonts you want and use the PostScript name of the font in your xcode project.

It should work even for different font variations of the same family.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文