SwiftUI won't display custom font

后端 未结 3 1701
猫巷女王i
猫巷女王i 2020-12-11 07:58

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

相关标签:
3条回答
  • 2020-12-11 08:02

    I had the exact same problem. The following steps fixed it for me. I'm currently using Xcode 11.4.1

    1. Create a storyboard, add a label and select your font in the inspector as font for the label.
    2. Build your App in the Simulator
    3. Check the installed fonts with:

      for family in UIFont.familyNames.sorted() {
          let names = UIFont.fontNames(forFamilyName: family)
          print("Family: \(family) Font names: \(names)")
      }
      
    4. If it's appearing you can use it also programmatically

    Here is also a list of Common mistakes with adding custom fonts

    0 讨论(0)
  • 2020-12-11 08:20

    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.

    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.

    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.

    0 讨论(0)
  • 2020-12-11 08:22

    I had the same issue,

    it worked for me when I omitted the "-Regular", but in the info.plist I wrote it with it.

    0 讨论(0)
提交回复
热议问题