简体   繁体   English

自定义字体仅在 Interface Builder 中设置时可用

[英]Custom Fonts only available when set in Interface Builder

I have added a custom font to my project.我在我的项目中添加了自定义字体。 It is included in the target, and it is added in the plist.它包含在目标中,并添加到 plist 中。 When I try to use it programmatically it doesn't work, and it doesn't show up when I print out a list of available fonts.当我尝试以编程方式使用它时,它不起作用,并且当我打印出可用字体列表时它也不会显示。 However, it does show up as an option in Interface Builder, and if I set a label's text to that font in IB, it works correctly and shows up when I print out a list of available fonts.但是,它确实显示为 Interface Builder 中的一个选项,如果我在 IB 中将标签的文本设置为该字体,它可以正常工作并在我打印出可用字体列表时显示。 This is XCode 6.4 and iOS 8.0这是 XCode 6.4 和 iOS 8.0

When it is working via IB, it gets printed out in the font names like this: Special Elite SpecialElite-Regular当它通过 IB 工作时,它会以这样的字体名称打印出来:Special Elite SpecialElite-Regular

I call the font programmatically like: [UIFont fontWithName:@"SpecialElite-Regular" size:fontSize];我以编程方式调用字体,如: [UIFont fontWithName:@"SpecialElite-Regular" size:fontSize];

There are no problems when doing this with the built-in fonts.使用内置字体执行此操作时没有问题。

When I try to use it programmatically it doesn't work, and it doesn't show up when I print out a list of available fonts当我尝试以编程方式使用它时它不起作用,并且当我打印出可用字体列表时它不显示

This proves that you have not in fact included it properly in the target and the Info.plist .这证明您实际上没有将它正确地包含在目标和Info.plist 中

The reason it seems to work in IB is that this font is also present on your computer .似乎在 IB 中工作的原因是该字体也存在于您的计算机上 But in fact if you were to run this app on your device, you would see that even setting the font in IB is not working.但事实上,如果你在你的设备上运行这个应用程序,你会发现即使在 IB 中设置字体也不起作用。

Your font is Special Elite.你的字体是 Special Elite。 As you can see, I have it visible here in my running app:如您所见,我在我正在运行的应用程序中可以看到它:

在此处输入图片说明

Here's the code that I used:这是我使用的代码:

    let lab = UILabel()
    lab.text = "This is a test"
    lab.font = UIFont(name:"SpecialElite-Regular", size:18)
    lab.sizeToFit()
    lab.frame.origin = CGPointMake(100,100)
    self.view.addSubview(lab)

So you see, it is possible to refer to this font in code — if it is loaded properly .所以你看,可能在代码中引用这个字体——如果它被正确加载 You are evidently not loading it properly.您显然没有正确加载它。 It's not in your app bundle, or it's not in the copy build phase, or it's not correctly listed in your Info.plist .它不在您的应用程序包中,或者不在复制构建阶段,或者未在您的Info.plist 中正确列出。

(Of course there's always a possibility that you're calling [UIFont fontWithName:size:] with a bad value for the name or for the size.) (当然,您总是有可能调用[UIFont fontWithName:size:]的名称或大小值不正确。)

Ok, topic is old but I would like to put in my two cents here.好的,话题很旧,但我想在这里投入我的两分钱。

First off all, read this article http://codewithchris.com/common-mistakes-with-adding-custom-fonts-to-your-ios-app/首先,阅读这篇文章http://codewithchris.com/common-mistakes-with-adding-custom-fonts-to-your-ios-app/

In brief:简单来说:

  1. remember to include your fonts to project请记住将您的字体包含在项目中
  2. setup target for fonts字体设置目标
  3. check if your fonts are included as resources in bundle检查您的字体是否作为资源包含在包中
  4. add custom fonts to plist file将自定义字体添加到 plist 文件
  5. check the real name of your font in code在代码中检查字体的真实名称

When I did everything like described in this article, my fonts works in interface builder only.当我按照本文所述进行所有操作时,我的字体仅在界面构建器中有效。 I also have problem with step 5 - my fonts did not show up in console output.我的第 5 步也有问题 - 我的字体没有显示在控制台输出中。

Ok, so now is time for tips from me:好的,现在是我提供提示的时候了:

TIP #1提示 #1

use otf font format, if you have different, convert it (or ask your designer ;) ) you can use this site for that: https://onlinefontconverter.com/使用 otf 字体格式,如果你有不同的,转换它(或询问你的设计师;))你可以使用这个网站: https : //onlinefontconverter.com/

TIP #2提示 #2

In article above you can find:在上面的文章中,您可以找到:

Open it and add a new row called “Fonts provided by application” which will be an array that you need to add all the filenames of the fonts you want to use.打开它并添加一个名为“应用程序提供的字体”的新行,这将是一个数组,您需要添加要使用的字体的所有文件名。

Instead of "Fonts provided by application" enter "UIAppFonts" Remember, if you have more than one plist file, set this for all.输入“UIAppFonts”而不是“应用程序提供的字体”请记住,如果您有多个 plist 文件,请为所有文件设置此项。

As mentioned by others, following article is very useful to diagnose the problem.正如其他人所提到的,以下文章对于诊断问题非常有用。 http://codewithchris.com/common-mistakes-with-adding-custom-fonts-to-your-ios-app/ http://codewithchris.com/common-mistakes-with-adding-custom-fonts-to-your-ios-app/

Also while adding custom fonts to Xcode project, add individual font and not the directory in which all fonts are stored.此外,在向 Xcode 项目添加自定义字体时,请添加单个字体,而不是存储所有字体的目录。 If you add that directory, fonts will be visible in Interface Builder but not in the running app.如果添加该目录,字体将在 Interface Builder 中可见,但在正在运行的应用程序中不可见。

Matt did a great job of isolating the problem... I was not loading the font correctly.马特在隔离问题方面做得很好......我没有正确加载字体。

In my case the problem was in the Info.plist.就我而言,问题出在 Info.plist 中。

I was listing the font in the Info.plist using its name.我在 Info.plist 中使用它的名字列出了字体。 This is incorrect.这是不正确的。 You are supposed to list the font in the Info.plist using its file name.您应该使用其文件名在 Info.plist 中列出字体

I know this is old but I just ran into the same issue.我知道这很旧,但我遇到了同样的问题。 The font would show up only if I selected that font in the interface builder.只有当我在界面构建器中选择了该字体时,才会显示该字体。 Turns out when I searched for UIAppFonts , it didn't list the main Info.plist (we were already using custom fonts) for some reason.结果当我搜索UIAppFonts ,由于某种原因它没有列出主要的 Info.plist(我们已经在使用自定义字体)。 So I ended up adding the fonts to the localized (?) plist file which resulted in this issue.所以我最终将字体添加到导致此问题的本地化 (?) plist 文件中。 What I had to do was to go to the Project Settings > Target > Info tab and add the font file names there.我必须做的是转到“项目设置”>“目标”>“信息”选项卡并在那里添加字体文件名。

Issue is You are not giving ProperName Of Fonts in Code, for Getting Name of Fonts please run this code.问题是您没有在代码中提供字体的专有名称,要获取字体名称,请运行此代码。

 for family: String in UIFont.familyNames
    {
        print("\(family)")
        for names: String in UIFont.fontNames(forFamilyName: family)
        {
            print("== \(names)")
        }
    }

it will prints all fonts, search your font and give proper name.它将打印所有字体,搜索您的字体并给出正确的名称。

 let attributedText = NSMutableAttributedString(string: "We 
 Apologize! \n This feature is only available for \n Go 4.0 
 Subscriptions", attributes: [NSAttributedStringKey.font : 
 UIFont(name: "SpecialElite-Regular", size: 22)!])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM