when I use a font awesome in swift I get just question mark even when I use different hex code like f002 , f4d7 and etc in u{}.
I also add property Font provided by application in info.plist
so what's your idea to solve it. in the below photo I put every think you may need to know :
1: check your font
for family in UIFont.familyNames {
print("\(family)")
for name in UIFont.fontNames(forFamilyName: family) {
print("\(name)")
}
}
2: maybe you have to use
Hex string to text conversion - swift 3
Related
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?
You don't need to include the group name in the font array, so just e.g.
<string>Poppins-Black.ttf</string>
I am using a custom font in my application. I added the fonts "ttf" file to the resource folder and made it available for all the target. I verified, it was in the copy bundle resource and in the plist file with proper naming conventions.
I used FontBook Mac app to take the PostScript name of the custom font and copy pasted to my code to avoid type errors.
I'm able to select the font in Storyboard/xib and its reflecting in the UI. But when I tried to do it programmatically like without using designers like below the font is not reflected in the UI.
The notable point here is I have 4 buttons and if I set font for one button via storyboard and set the same font for other buttons via programmatically, then its working.
exploreAppBtn.titleLabel?.font = UIFont(name: "SansOfcMed-Bold", size: 20)
Is it an Xcode issue? Anyone have faced it? If yes, whats the solution?
Added the Plist file and Build phases screenshots
Any help is appreciated! Thanks
Check if custom font is added in info.plist file with proper naming conventions.
Add extension
extension UIFont {
class func fontName(size: CGFloat) -> UIFont {
return UIFont.init(name: "CustomFontName", size: size)!
}
}
Usage:
exploreAppButton.titleLabel?.font = UIFont.fontName(size: 16)
Could you please clarify how exactly you create CustomFontName
Maybe the font name is not valid
Valid fonts name you can find with the code.
let familyFontsNames = UIFont.familyNames
for familyName in familyFontsNames {
let fontsName = UIFont.fontNames(forFamilyName: familyName)
for fontName in fontsName {
print(fontName) // Prints list of all available fonts name. And you should use the name.
}
}
Try to check if your CustomFontName exists in the list.
I'm trying to add a custom font to my iOS app and I have followed all the necessary steps(plist, copy build resources etc). The font is working in the Builder Interface but when I run it all that shows up is boxes with ? in them. Please help this has been an issue for almost a week now.
!(https://imgur.com/a/rO50c9Q)
Make sure you checked Target Membership
From what you described you seemed to have followed the correct steps.
Maybe it's the font name that's causing the issue?
According to this tutorial: Add Custom Fonts To Your App
the font name may not be what you expect. It could be very different
than any of the visible font names that you can see
He says add this code in viewDidLoad to of any of your view controllers to find all the font names that your app is currently using. There is a possibility the font name you entered might be different from the font name the app has it listed as:
override func viewDidLoad() {
super.viewDidLoad()
for family: String in UIFont.familyNames {
print("\(family)")
for names: String in UIFont.fontNames(forFamilyName: family)
{
print("== \(names)")
}
}
}
I added new font - 'SourceSansPro'(got from GoogleFonts) to my React-Native project. But I got 'Unrecognized font family' error on IOS simulator. There are dozens of various suggestions about this problem like deleting build items or look whether fonts are defined in info.plist or execute 'react-native-link' and etc.However, none of them worked for my case. Also, some people mentioned that, although it does not work through CLI, it works when executed directly through Xcode. Whereas,it did not work for me. I'm stucked at this problem for almost 5 hours. Finally, I thought maybe someone may helps me, here.
Do you have any suggestion?
Your font's name is probably not SourceSansPro
Depending on which one (or more than one) that you added to your project, the name is more likely SourceSansPro-Regular or SourceSansPro-Bold or SourceSansPro-Italic etc
I don't use React-Native, but you can list the Font Families and Font Names available to your app with this code:
for (NSString* family in [UIFont familyNames])
{
NSLog(#"%#", family);
for (NSString* name in [UIFont fontNamesForFamilyName: family])
{
NSLog(#" %#", name);
}
}
You need to also make sure that the fonts are included as part of the bundle in the iOS project. First drag the font to the file navigator in Xcode under your project. Then select the font in the file navigator, open the Inspector and make sure it's ticked for your project under Target Membership. Hope this helps.
Please add your custom font to your Plist file like the attached screenshot with key "Fonts provided by application" . And please make sure that the font is successfully loaded by change the font name to custom font from xib , you should see your custom font at the list of fonts at xib .
Thank you
Nada Gamal
The Swift version of the DonMag's answer:
for family: String in UIFont.familyNames{
print("\(family)")
for names: String in UIFont.fontNames(forFamilyName: family){
print("== \(names)")
}
}
I am currently using many different fonts. All of them are working except for one particular.
I added it in the same way as I added others which are working:
Import the font into project
Modify info.plist
Find proper font family name (I did this by plugging NSLog inside RCTConvert)
Use the font in react-native through CSS with fontFamily
Unfortunately I cannot share the font publicly, so I have to ask how would one debug this issue?
One possible culprit is the font weight. I was trying to use a font with a weight of 325 at one point, but React Native seems to not allow the usage of a font unless the weight falls within their enum values (normal, bold, all hundreds within the range 100-900).
I used this app to modify the font weight of the file I was trying to use and set it to a standard 400: https://glyphsapp.com
In AppDelegate.m, under NSURL *jsCodeLocation, paste the following code. This will log out all available fonts, including the new font(s) that you have added:
for (NSString* family in [UIFont familyNames])
{
NSLog(#”%#”, family);
for (NSString* name in [UIFont fontNamesForFamilyName: family])
{
NSLog(#” %#”, name);
}
}
That way, you can get the exact font name to use in your app. The log output should look something like this:
I needed the same solution, using Swift. Here is the script to print out the Font Familes, and Faces, in Swift (works in appDelegate)
let fontFamilies = UIFont.familyNames()
for fontNames in fontFamilies{
print(fontNames)
let fontFace = UIFont.fontNamesForFamilyName(fontNames)
for aName in fontFace{
print(" \(aName)")
}
}[output of script][1]
link to part of log...http://www.zonesight.com/stackImages/font.png