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

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

问题描述

我目前正在尝试向我的项目添加自定义字体,但不知何故无法工作.

我已经将 .otf 文件添加到字体文件夹中,检查它是否以我的项目为目标,并将 应用程序提供的字体 添加到 Info.plist.

struct ContentView:查看{var主体:一些视图{文本(最近").foregroundColor(颜色(主要")).font(Font.custom("HelveticaNowDisplayBold", size: 60))}}

解决方案

如果您确定 Info.plist 使用了正确的文件名:

<块引用>

请注意,如果您使用的是 Xcode 13,则可能没有您期望的 Info.plist.这个

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

检查字体名称的简单方法是在 return true 之前将以下内容添加到 didFinishLaunchingWithOptions 中的 AppDelegate.或者,如果您正在使用新的 SwiftUI 生命周期,您可以将其添加到 .onAppear.

用于 UIFont.familyNames.sorted() {让名称 = UIFont.fontNames(forFamilyName: family)打印(家庭:(家庭)字体名称:(名称)")}

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

请记住在使用完后将其删除,因为您不需要不必要地打印到控制台.

当我为我的字体做这件事时(我添加了与你相同的字体)我在控制台的可用字体列表中找到以下内容(见上面的截图):

系列:Helvetica 现在显示字体名称:[HelveticaNowDisplay-Bold"]

您的字体可能与我的名称不同,请务必注意字体名称可能与文件名不同.这是很多人的绊脚石,因为他们在需要使用字体名称时尝试使用文件名.

以下测试代码产生:

struct ContentView:查看{var主体:一些视图{文本(你好").foregroundColor(.blue).font(Font.custom(HelveticaNowDisplay-Bold",大小:60))}}

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


SwiftUI 中的动态类型

如果您使用的是自定义字体,那么您应该考虑对其进行设置,使其随动态字体缩放.

iOS 14

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

Text(你好").font(.custom(HelveticaNowDisplay-Bold",大小:60,relativeTo:.body))

iOS 13

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

您首先需要创建一个 ViewModifier.这个视图修饰符从环境中监听尺寸类别(它实际上并不使用它,但在此处使用它可以确保在尺寸类别更新时更新视图修饰符).

struct ScaledFont: ViewModifier {@Environment(.sizeCategory) var sizeCategory变量名:字符串变量大小:CGFloatfunc 正文(内容:内容)->一些视图{让 scaledSize = UIFontMetrics.default.scaledValue(for: size)返回 content.font(.custom(name, size: scaledSize))}}扩展视图{func scaledFont(name: String, size: CGFloat) ->一些视图{返回 self.modifier(ScaledFont(name: name, size: size))}}

然后按如下方式使用:

Text(你好").scaledFont(名称:HelveticaNowDisplay-Bold",大小:60)

如果想写得很好,请查看 使用 Swift 进行黑客攻击.

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

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))
    }
}

解决方案

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

Note if you are using Xcode 13 you may not have an Info.plist where you expect. This SO answer explains where you can find it.

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.

An easy way to check the font's name is to add the following to your AppDelegate in the didFinishLaunchingWithOptions before the return true. Or if you are using the new SwiftUI lifecycle you can add it to an .onAppear.

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.

Just remember to remove it once you have finished using it as you don't need to unnecessarily print to the console.

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, and it is important to note that the font name may not be the same as the filename. This is what trips up a lot of people, as they try using the filename when they need to use the font name.

The following test code produces:

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

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


Dynamic Type in SwiftUI

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

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

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天全站免登陆