SwiftUI不会显示自定义字体 [英] SwiftUI won't display custom font

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

问题描述

我当前正在尝试向项目中添加自定义字体,但是我不知怎么工作.

I'm currently trying to add a custom font to my project, but I somehow won't work.

我已经将.otf文件添加到了字体文件夹中,检查了它是否以我的项目为目标,并且将Fonts provided by application添加到了Info.plist中.

I've already added the .otf file to a font folder, checked that it targets to my project and added Fonts provided by application to Info.plist.

struct ContentView: View {
    var body: some View {
        Text("CLOSEST")
          .foregroundColor(Color("primary"))
          .font(Font.custom("HelveticaNowDisplayBold", size: 60))
    }
}

推荐答案

如果您已确保Info.plist使用了正确的文件名

If you have made sure that the Info.plist is using the correct filename

,并且该字体在应用程序的目标中可用.

and that the font is available in the app's target.

您还需要确保您使用正确的名称访问字体.

You also need to make sure that you are accessing the font by the correct name.

一种简单的检查字体名称的方法是在return true之前的didFinishLaunchingWithOptions中将以下内容添加到AppDelegate中.

An easy way to check the font's name is to add the following to your AppDelegate in the didFinishLaunchingWithOptions before the return true.

for family in UIFont.familyNames.sorted() {
    let names = UIFont.fontNames(forFamilyName: family)
    print("Family: \(family) Font names: \(names)")
}

这将按族和名称列出所有字体.

This will list all the fonts by family and name.

当我对字体进行处理时(我已经添加了与您相同的字体),我在控制台中的可用字体列表中找到了以下内容(请参见上面的屏幕截图):

When I do it for my fonts (I have added the same font as you) I find the following in the console in the list of available fonts (see the above screenshot) :

Family: Helvetica Now Display Font names: ["HelveticaNowDisplay-Bold"]

您的字体可能要使用其他名称.

Your font may have a different name to mine.

以下测试代码产生:

struct ContentView: View {
    var body: some View {
        Text("Hello")
            .foregroundColor(.blue)
            .font(Font.custom("HelveticaNowDisplay-Bold", size: 60))
    }
}

有关添加自定义字体的更多信息,请参阅Apple的文档.

For more information about adding custom fonts see Apple's documentation.

如果您使用的是自定义字体,则应考虑对其进行设置,以使其能够以动态字体缩放.

If you are using a custom font then you should consider setting it up so that it will scale with dynamic type.

iOS 14引入了一个新的修饰符,使您可以相对于动态字体类型缩放字体.

iOS 14 introduces a new modifier that allows you to scale a font relative to a Dynamic Font Type.

Text("Hello")
    .font(.custom("HelveticaNowDisplay-Bold", size: 60, relativeTo: .body))

iOS 13

如果您使用的是iOS 13,则需要更多的努力才能获得相同的效果.

iOS 13

If you are using iOS 13 that requires a bit more effort to get the same effect.

您首先需要创建一个ViewModifier.这个视图修饰符从环境中侦听尺寸类别(实际上并没有使用它,但是在这里放置它可以确保每当更新尺寸类别时都会更新视图修饰语).

You first need to create a ViewModifier. This view modifier listens to the size category from the environment (it doesn't actually use it but having it here makes sure the view modifier is updated whenever the size category is updated).

struct ScaledFont: ViewModifier {
    @Environment(\.sizeCategory) var sizeCategory
    var name: String
    var size: CGFloat

    func body(content: Content) -> some View {
       let scaledSize = UIFontMetrics.default.scaledValue(for: size)
        return content.font(.custom(name, size: scaledSize))
    }
}

extension View {
    func scaledFont(name: String, size: CGFloat) -> some View {
        return self.modifier(ScaledFont(name: name, size: size))
    }
}

然后按以下方式使用它:

It is then used in the following way:

Text("Hello")
    .scaledFont(name: "HelveticaNowDisplay-Bold", size: 60)

要获得非常好的写作,请在

For a really good write up check out this post on Hacking With Swift.

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

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