I have loaded custom fonts into my SwiftUI project in Xcode to a folder, with the target membership set to the correct build, I have included the correct names inside the info.plist in the fonts provided by application section, and included them in the code using the correct names as such:
extension Font {
static let mainFont: (FontWeights, CGFloat) -> Font = { fontType, size in
switch fontType {
case .hairline:
Font.custom("TT Norms Pro Thin", size: size)
case .thin:
Font.custom("TT Norms Pro ExtraLight", size: size)
case .light:
Font.custom("TT Norms Pro Light", size: size)
case .regular:
Font.custom("TT Norms Pro Regular", size: size)
case .medium:
Font.custom("TT Norms Pro Medium", size: size)
case .semiBold:
Font.custom("TT Norms Pro DemiBold", size: size)
case .bold:
Font.custom("TT Norms Pro Bold", size: size)
case .heavy:
Font.custom("TT Norms Pro ExtraBold", size: size)
case .black:
Font.custom("TT Norms Pro Black", size: size)
}
}
static let serifFont: (SerifFontWeights, CGFloat) -> Font = { fontType, size in
switch fontType {
case .semiBold:
Font.custom("Butler Free Bd", size: size)
}
}
}
extension View {
func mainFont(_ fontWeight: FontWeights? = .regular, _ size: CGFloat? = nil) -> some View {
return font(.mainFont(fontWeight ?? .regular, size ?? 16))
}
func serifFont(_ fontWeight: SerifFontWeights? = .semiBold, _ size: CGFloat? = nil) -> some View {
return font(.serifFont(fontWeight ?? .semiBold, size ?? 16))
}
}
Most of the time this works and the font shows, but if I switch applications, often some of the fonts disappear and the standard iOS font shows. Sometimes, but rarely, it also already doesn't show correctly on launch. I also tried simpler ways to include the font and different font files, but it causes the same behavior. What could be the reason for this erratic behavior?
Somewhere in my code I had the following and this caused all the fonts to sometimes not load correctly:
Text("Existing user?") .font(.body) + Text(" Sign in") .font(.body) .fontWeight(.semibold)
Changing this to two separate text elements resolved all the font issues elsewhere as well