First I am aware of the fact that questions like this have been asked before but having reviewed all of the answers I am still having issues.
I am writing my first project in SwiftUI using Xcode 14.1 and wanted to add a custom font. I first created a Fonts
group within the project and added the .ttf files to the project using Add Files to "Project"
ensuring that Copy items if needed
was checked.
I then went and added the Fonts provided by application
to the Custom iOS Target Properties
and the array items for the font in this case Poppins
so the values read Fonts/Poppins-Regular.ttf
<key>UIAppFonts</key>
<array>
<string>Fonts/Poppins-Black.ttf</string>
<string>Fonts/Poppins-BlackItalic.ttf</string>
<string>Fonts/Poppins-Bold.ttf</string>
<string>Fonts/Poppins-BoldItalic.ttf</string>
<string>Fonts/Poppins-ExtraBold.ttf</string>
<string>Fonts/Poppins-ExtraBoldItalic.ttf</string>
<string>Fonts/Poppins-ExtraLight.ttf</string>
<string>Fonts/Poppins-ExtraLightItalic.ttf</string>
<string>Fonts/Poppins-Italic.ttf</string>
<string>Fonts/Poppins-Light.ttf</string>
<string>Fonts/Poppins-LightItalic.ttf</string>
<string>Fonts/Poppins-Medium.ttf</string>
<string>Fonts/Poppins-MediumItalic.ttf</string>
<string>Fonts/Poppins-Regular.ttf</string>
<string>Fonts/Poppins-SemiBold.ttf</string>
<string>Fonts/Poppins-SemiBoldItalic.ttf</string>
<string>Fonts/Poppins-Thin.ttf</string>
<string>Fonts/Poppins-ThinItalic.ttf</string>
</array>
However, when printing out my fonts in an init()
method that calls this function within my App
swift file:
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)]")
}
}
It Doesn't output my custom font, equally when using the font in my view it doesn't render:
Text("Hello!")
.font(Font.custom("Poppins-Regular", size: 18))
Interestingly the preview in Xcode doesn't work since adding this property to the Text in the view reporting the following error: Compiling failed: ambiguous use of 'font'
I have searched and searched and try a number of rabbit hole solutions can any one advise?
CodePudding user response:
You don't need to include the group name in the font array, so just e.g.
<string>Poppins-Black.ttf</string>