Why Custom fonts are not applied in Cocoa Application - swift

Im working on Mac OS Application where I need to set custom fonts, so I did following things:
My Xcode version: 12.3
Added fonts to Application
Add Application fonts resource path Key to Info.plist and set the value "."
Added "New Copy file phase" and provided path Resource->Fonts and added fonts.
In my viewController set those fonts to Label programatically. Like this (titleLabel.font = NSFont(name: "Geogrotesque-Regular", size: 15))
Everything's works fine (fonts getting applied) when I run it at my end. But when I package it through archive or direct through product app folder and send it to my colleague. it doesn't work at their end. It seems very strange things are happening I gone through lot of articles and as per various suggestion I verified my implementation it seems no issue. Then what Im missing here?

Related

Custom font not loading in SwiftUI project after moving code to another project?

I had a SwiftUI project that I essentially messed up pretty bad and had to make a new project and transfer all the files over. It's working as intended except for my custom fonts aren't working and the default font is taking over. Here is what I did:
Copied my fonts folder into my new project
Checked the Target Membership box for my project and made sure that the path to the directory was referring to the new path, not the old path (bc it was before I changed it).
Included the names of both fonts in my plist, exactly how they were in the initial project's plist, in the fonts provided by application array.
Deleted and readded the fonts folder in my project's Build Phases (again, to make sure it was referring to the correct directory).
And yeah, still not working. Any insight as to what I could be overlooking? Because it was working completely as intended before I had to create the new project, and all other elements are working as intended.
If you are struggling with this in 2022 and using SwiftUI where there seems to be no info.plist in sight, then in addition to adding the Fonts capability in Target Signing and Capabilities, then dragging the file into your project and don't forget to check Add to Targets, and finally add your InsertFontFileName.ttf to "Fonts provided by application" which is now in your Info tab on the Target as seen in the image below. Also, call me crazy but before you drag the font in remove any special characters. In addition, don't be fooled by the auto-generated info.plist, add your new fonts via the image below, it'll auto-populate to that other plist. Maybe some of my steps are superstitious, but I spent enough time futzing around with fonts that I'm done thinking about it and I'll just follow my formula... and if that font doesn't work, it wasn't meant to be and I'll move onto the next font!
After that
.font(Font.custom("InsertFontFileName", size: 32, relativeTo: .title))
Although it looks like you have already done this, just go through the following checklist once:
Fonts (.ttf files) are present inside the project
Entry in info.plist file
Entry in "Copy Bundle Resources" under "Build Settings" of your target
If this is missing, add by clicking on the plus (+) icon in this section
Make sure you are using the font correctly (check spelling errors?)
var FontRegular : Font = Font.custom("Poppins-Regular", size: 16)
var FontBold : Font = Font.custom("Poppins-Bold", size: 16)
...
Text("Sample Text")
.font(FontRegular)

Adding a custom font to macOS app using Swift

I followed a bunch of tutorials but it doesn’t work: I simply want to add a custom font to a macOS app.
What I tried essentially:
Added the .ttf font-files to my project: Target Membership is set and I also made sure that the files are copied using Copy Files within Bundle Phases. After compiling I can see that all files are within the Bundle. So that seems to work perfectly fine.
Info.plist: I added Fonts provided by application and created an item for every font-file I want to add (values like myFont.tff).
I made sure that I use the correct font name. I installed the fonts on my system and configured a Label with Interface Builder so that it uses the desired font. I printed it’s value print(myLabel.font.fontName).
Confusing: If the exactly same font-file I want to add to the app is installed in the systems Fontbook and activated, everything works well. When I deactivate it, it doesn’t work. For me that indicates that I am using the correct font name.
I found ATSApplicationFontsPath and tried to add it to the Info.plist, but neither using a path (recommended by the docs) or values like . (which seemed to work for some people out there) worked out.
Appreciate any help!
I had a hard time getting this to work based on the previous answers, so I decided to write up an answer myself. This is based on previous answers by #ixany and #rob-keniger.
Create a folder named Fonts
Add fonts to the Fonts folder. Uncheck Add to targets and check Copy items if needed.
Add Application fonts resource path to Info.plist and enter Fonts.
Go to Build Phases and create a New Copy Files Phase.
Set the Destinations to Resources and Subpath to Fonts. Then add your font files to the list.
I solved my problem by accident.
I noticed that within the Bundle files, my fonts were included twice: They were copied to the "Resources" directory as well as to the subpath I declared in the "Copy Files" Build Phases.
I removed the fonts from my project, added them again (without checking the "Target Membership") and added them in the Build Phases so they were copied just to the subpath. After that it worked.
Additional notes:
Turned out that Fonts provided by application is a iOS only property so it is not needed for an macOS implementation.
Also, ATSApplicationFontsPath does not need any slashes. For example: Fonts should work just fine.
I had a difficult time finding the correct names for .ttf font files and sometimes discovered some strange behaviour: NSFont picked randomly fonts (regular, medium, bold...) from a font family without any code changes. By using .otf files instead I could solve that too.
None of the above worked for me in Xcode 12 beta 1 running BigSur but these steps did.
Create a Fonts folder for organization.
Add your fonts to the project. Be sure to check the macOS target!
Set Application fonts resource path to "." (no quotes).
No need to edit build phases or anything else.
THe value ATSApplicationFontsPath (i.e. Application fonts resource path) is relative to the Resources folder of your app bundle.
To see where your fonts are copied to:
In Xcode, expand the Products section under Project Navigator and click Show in Finder
In Finder, Ctrl-click on your app and click Show Resource Contents. Browse to the Resources folder and find your fonts there.
Xcode 12 to add custom fonts you have to exclude extension name from the string provided.
i.e.
.font(.custom("your font name", size:xx)) // works every time
.font(.custom("your font name.extension",size:xx)) // always fails
same is not true with Xcode 11.xx there fontname.extension is working.

UIFont Woes (some custom fonts loading, but other's are not)

I'm having an issue getting certain custom fonts to load. I followed the advice in the traditional, 400+ upvoted answer to this question, and it worked for one project perfectly. However, in a different project I am working on I have run into issues with loading a UIFont.
These issues are somewhat similar to the issues found in the post Adding multiple fonts to UIAppFonts overrides each other. The solution there was that there were issues with the p-list properly parsing out the names into XML leading to overwriting. I have attempted to test that hypothesis of the issue:
I listed the fonts in my p-list under
UIAppFonts (which turns into "Fonts provided by application")
Item 0 - Merriweather-Regular.ttf
Item 1 - OpenSans-Regular.ttf
Item 2 - OpenSans-Bold.ttf
The last of which (OpenSans-Bold) was the only one that loaded. I have since tried moving the items around to no effect. I have even deleted OpenSans-Bold.ttf from the p-list and the project and tried running it. Clearly then they are not "overwriting each other." All of the files were copied into my bundle copy (another common error) and still, the only file that registered was OpenSans-Bold in any of my views. I've deleted everything and added it back in... basically done all traditional "weird XCode could cause the problem" bug testing and it hasn't changed anything. Anyone have any idea what the issue could be?
Thanks a lot!
The answer to this is that the font file name differs from the font name. If the file name is something like OpenSans-Regular, you sometimes only need to do the font name OpenSans when calling fontWithName:. This is why "OpenSans-Regular" did not work (as it was not the font name), whereas "OpenSans-Bold" did (since the name was the same as the file.)

Viewing Custom Text in Xcode Storyboard

I have my custom fonts working fine when they are running on the simulator, but is there any way to view custom fonts in the storyboard itself while I'm working? My text doesn't match the size of the default font so I have to keep compiling and running the code and eyeing the correct size by trial and error over and over.
I've seen a few questions hit on this, but it's just bringing up how to upload custom fonts.
You can't do it in Xcode 4, as it limits you to the preloaded fonts. Xcode 3 had the opposite problem (with .xib files anyway) of allowing you to use any font on your Mac, even if it would not be available to your app at runtime.
Hopefully a future version of Xcode will hit the useful middle ground of giving you exactly what it can determine will be available at runtime. This is actually difficult for Apple to do because the storyboards/nibs live in a project (which may have multiple targets) and the fonts exist in the targets. IMO they should let you use any font and add a build-time warning if a storyboard/nib is built into a target that does not have all the required fonts in it.
What may work is to edit the storyboard as text (available by right-clicking) and rewrite the font names in there to your custom font names. If it follows the pattern of other Apple products you will then see the custom fonts but it will "correct" them back to "legal" fonts if you access that element in the inspector. I haven't tried this.
My pragmatic technique is to find a system font which has roughly the same characteristics in terms of character dimensions and leading, and use that in the storyboard for layout purposes.

Why is app icon white?

Some recent version of iOS caused my app icon to turn white. I've gone through everything in regards to images and can't find the issue. I'm running Xcode 3.2.5 with sdk 4.2. I do have an entry for "Icon files" in Info.plist with 5 keys:
Item 0 myapp.png
Item 1 myapp#2x.png
Item 2 myapp-72.png
Item 3 myapp-Small-50.png
Item 4 myapp-Small.png
Item 5 myapp-Small#2x.png
All the above have proper dimensions.
I see the image in 'Copy Bundle Resources' and also in the .app file for the simulator version. It is properly reference by name in 'Icon files', Item 0. It is not named Default.png since it is referenced by name in Item 0.
Any suggestions?
You just need those 3 Files:
Icon.png
Icon#2x.png
Icon-iPad.png
Put the names into the Info.plist under Icon Files.
EDIT: Clean your target if it still won't work!
That is what helped me (project summary - app icons are ok, iOS simulator - app icons are ok, iPhone device - blank icon).
Just reconnect your icons to project by dragging them from project navigator to App icon section in project summary.
When does it turn white? If you upgrade an app when it's running, sometimes it appears with a white icon in the "task" bar. This applies to apps you're developing and those download from iTunes. The fix is usually just to kill the app and restart.
That's the simplest answer, but I suspect might not be the solution.
Does the case of the file names in your Info.plist match the actual file names? The Simulator is not case sensitive but the iOS is.
You might also like to make a completely clean build. Don't just do a "Clean" in Xcode, instead manually clear out your build folder.
Finally, when the iPhone 4 first came out I had some issues with the order of the icons referenced in the Info.plist. I never saw white icons (usually just the non-Retina version) but this may be something worth playing with if nothing else works.
The icon file name may be too long if it is a custom file name. This is just a guess, and I hate guessing, but this appears to have been the problem I just had.
I have four targets using the same code but with different icons. I named the icons differently based on the app name, so I could store them in the same directory.
For example:
Icon.png
Icon_second.png
Icon_alternate.png
Icon_second_alternate.png (or Icon_secondalternate.png in case underscores were an issue)
This nomenclature was used for all of the icon names (there's 8 icon files to support iPhone, iPad, retina, etc).
The last one was giving me a problem. Despite a hundred clean/rebuilds, deleting the schema entirely and creating a new one, fruitless efforts, it suddenly occurred to me to try renaming it.
You should be able to name the icons differently, as I still use the first three versions. They are listed in the plist file and render in the project summary, and on the simulator, just fine. The last one rendered in the summary as well. However, I think the length of the file name was too long for the simulator. There must be a length limit. I renamed the fourth one to use the standard Icon.png naming but added it to the fourth target only (images are stored in a separate folder).
CONCLUSION: If you have more than one target for your app, use the standard naming system for all of your icons but add them to separate folders, and then when adding to Xcode just set the build target appropriately.
If you still have the default icon entry (even blank), this can happen, remove that entry in info.plist.
Also, Default.png would be your splash screen.
Load the icons into the Preview application, and make sure they are really png format (and not just named such) by doing a "Save As..." specifying PNG format in the save dialog. Do a Get Info to check the sizes. Make sure the case of all letters in the name match your plist entries.
I had the same problem recently - the Icons I was using were created in GIMP. When I opened them in Photoshop on the development Mac it complained about an ICC color profile. After re-saving the PNGs in Photoshop the Icons worked like a treat.
P.s. I just repeated my steps in creating the images in GIMP, but when saving unticked every extra option that GIMP - by default - was trying to add into the image when saving it. The icons worked perfectly this time around.
If you believe your icon configuration is correct, then you may simply be seeing a bug in Xcode.
As a workaround…
"Clean" your project. Choose Xcode > Product > Clean.
Delete the app from your simulator, or even reset the Simulator by choosing iOS Simulator > Reset Content and Settings.
Quit both the iOS Simulator and XCode apps, then restart them.