简体   繁体   English

swift 4 中的自定义字体导致致命错误:在展开可选值时意外发现 nil

[英]Custom font in swift 4 make Fatal error: Unexpectedly found nil while unwrapping an Optional value

I have to add custom font to my app.我必须向我的应用程序添加自定义字体。 I added fonts to my project and listed infolist.我在我的项目中添加了字体并列出了 infolist。

 enum Proxima: String {
        case regular = "Regular"
        func font(size: CGFloat) -> UIFont {
            return UIFont(name: "Proxima-\(rawValue)", size: size)!
        }
    }

Fatal error: Unexpectedly found nil while unwrapping an Optional value while calling致命错误:在调用时打开 Optional 值时意外发现 nil

self.headTitle.font = UIFont.Proxima.regular.font(size: 30)

UPDATE更新

I added the target and also checked the copy bundle resources all are correct but still i got same error.我添加了目标并检查了复制包资源都正确,但我仍然遇到同样的错误。

在此处输入图像描述 在此处输入图像描述

info.plist信息列表

Be sure that your files are added to your target确保您的文件已添加到目标

Also, don't forget to add them into the "Font provided by application" section of the Info.plist另外,不要忘记将它们添加到Info.plist的“应用程序提供的字体”部分

If you follow this Apple guide and still have problems it is most certain that the problem is with the font name.如果您遵循此Apple 指南但仍有问题,则最确定问题出在字体名称上。 To check the correct font name, install the font on your Mac, and open it.要检查正确的字体名称,请在 Mac 上安装该字体,然后打开它。 Then click the desired font and then the "i" icon for more information:然后单击所需的字体,然后单击“i”图标以获取更多信息:

在此处输入图像描述

Make sure that this is the name you use in both:确保这是您在两者中使用的名称:

  • Info.plist信息列表
  • In your enum (the raw value)在您的枚举中(原始值)

在此处输入图像描述

在此处输入图像描述

PS: If you try to use licensed fonts, then it will not work with the method above. PS:如果您尝试使用授权字体,则无法使用上述方法。 I tried to use Bariol (duh!), but it would not recognize them.我尝试使用 Bariol(呃!),但它无法识别它们。 I assume there is a way to integrate your license, but I did not spend the time to do it, I just switched to a Google Font instead.我认为有一种方法可以集成您的许可证,但我没有花时间去做,我只是改用了 Google 字体。

It seems that you have not added fonts into your target看来您还没有在目标中添加字体

Select your fonts and from right panel check the target选择您的字体并从右侧面板检查目标

在此处输入图像描述

Remove Regular for font because iOS uses a default font as regular try modifying code your code and It's working fine after that.删除常规字体,因为iOS使用默认字体作为常规尝试修改代码代码,之后它工作正常。

Ex:前任:

@IBOutlet weak var label1: UILabel!
enum OpenSans: String {
    case regular = ""
    case semiBold = "-Semibold"
    func font(size: CGFloat) -> UIFont {
        return UIFont(name: "OpenSans\(rawValue)", size: size)!
    }
}

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib..

    label1.font = OpenSans.regular.font(size: 30.0)

    label1.font = OpenSans.semiBold.font(size: 30.0)
}

If still you have a problem Please refer to this demo project如果还有问题请参考这个演示项目

FontDemo 字体演示

Hope it will help.希望它会有所帮助。

Try these试试这些

  • Add custom files in your project and also verify in Copy Bundle Resource in Build Phase在您的项目中添加自定义文件,并在 Build Phase 的 Copy Bundle Resource 中进行验证

  • Using this function you can check fonts available in your project.使用此功能,您可以检查项目中可用的字体。 add this function in appDelegate and call it from didFinsihLaunching.在 appDelegate 中添加此函数并从 didFinsihLaunching 中调用它。

     func printFonts() { let fontFamilyNames = UIFont.familyNames for familyName in fontFamilyNames { print("------------------------------") print("Font Family Name = [\(familyName)]") let names = UIFont.fontNames(forFamilyName: familyName) print("Font Names = [\(names)]") } }
  • If your custom font is available in printed fonts list then you can use.如果您的自定义字体在打印字体列表中可用,那么您可以使用。

Run the following code to check available font in your project.运行以下代码以检查项目中的可用字体。

for fontFamilyName in UIFont.familyNames{
    for fontName in UIFont.fontNames(forFamilyName: fontFamilyName){
        print("Family: \(fontFamilyName)     Font: \(fontName)") 
    }
}

See the output in your console, all the available fonts are printed here, check the actual font name of your provided fonts.查看控制台中的输出,所有可用字体都打印在这里,检查您提供的字体的实际字体名称。

And use that font name for UIFont(name: "FontName", size: 15)并将该字体名称用于UIFont(name: "FontName", size: 15)

Xcode 11.1, iOS 13.1 Xcode 11.1、iOS 13.1

I did all things right, but one custom font was missing in the list of fonts (via UIFont.fontNames(forFamilyName: fontFamilyName) ).我做对了所有事情,但是字体列表中缺少一种自定义字体(通过UIFont.fontNames(forFamilyName: fontFamilyName) )。

Then I reordered the missing font in Info.plist (Fonts provided by application) to the front and it worked, yay.然后我将 Info.plist(应用程序提供的字体)中缺少的字体重新排序到前面,它起作用了,是的。

Maybe this helps someone.也许这对某人有帮助。

转到身份并输入并检查是否为项目添加字体

暂无
暂无

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

相关问题 Swift:致命错误:在展开可选值时意外发现nil - Swift : fatal error: unexpectedly found nil while unwrapping an Optional value 快速致命错误:解开Optional值时意外发现nil - swift fatal error: unexpectedly found nil while unwrapping an Optional value Swift致命错误:解开Optional值时意外发现nil - Swift fatal error: unexpectedly found nil while unwrapping an Optional value 致命错误:在Swift 3中解开Optional值时意外发现nil - fatal error: unexpectedly found nil while unwrapping an Optional value in Swift 3 SWIFT-致命错误:解开可选值时意外发现nil - SWIFT - fatal error: unexpectedly found nil while unwrapping an Optional value 致命错误:解开可选值(cellForRowAtIndexPath)时意外发现nil(Xib自定义单元格Swift) - fatal error: unexpectedly found nil while unwrapping an Optional value (cellForRowAtIndexPath) (Xib Custom Cell Swift) Swift-致命错误:解开Optional值时意外发现nil - Swift - Fatal error: unexpectedly found nil while unwrapping an Optional values Swift中的可选类型错误:致命错误:解开可选值时意外发现nil - Optional type error in Swift: fatal error: unexpectedly found nil while unwrapping an Optional value Xcode Swift:appDelgate.window! is nil :致命错误:在解开可选值时意外发现 nil - Xcode Swift : appDelgate.window! is nil : Fatal error: Unexpectedly found nil while unwrapping an Optional value 致命错误:在Tableview中展开Optional值时意外发现nil - fatal error: unexpectedly found nil while unwrapping an Optional value in Tableview
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM