When learning a new language or environment, some things that should be trivial seem to take forever. Once you learn how to do simple tasks, it’s hard to remember how you originally struggled. So for that reason, I’ve decided to keep a running log of useful code snippets that were stumbling blocks at some point. Anything that I think I’ll reuse, or had trouble understanding as a beginner will be added as a gist to my Github account.
A standard example would be adding video to a Sprite Kit project. Sure, it’s easily found in the official docs, but the docs are a bit intimidating when you’re starting out.
Adding Custom Fonts
Adding a custom font turned out trickier than I anticipated because the font wasn’t recognized as a resource just by adding it to the project (which is what happens with audio, images, etc). I found a perfect answer on stack overflow that sums up what is needed to get custom fonts working.
- Go to supporting files -> ProjectName.plist. Add am array key of “Fonts provided by application” and place one element in the array. The element should be a string with a value of your font file name. In my case, dinconra.ttf.
- At the top-center of your Xcode window you should see a navigator that starts with your project name. Click on the project name. In the new window that appears, there should be a tab to the right called “Build Phases”. Click that, and expand “Copy Bundle Resources.” Finally, add your font as a resource by clicking the plus sign in the expanded section.
- Lastly, and most confusingly, find out the real name of your font. You can do this in finder by opening the font in Font Book and looking at the title bar on the top of the window that displays the font. In my case, the font name is actually “DINCond-RegularAlternate” instead of “dinconra.”
Delayed Function Calls with Parameters
It is common to see example code calling functions on a delay using performSelector, but the syntax is weird when you need to pass a parameter into the target function. At least, weird if you’re coming from webdev languages.
As you can see in the snippet, a colon is necessary after the function name (doImageFade) in order to use the withObject parameter.
Be sure to check in on my gists every once in a while if you’re learning Sprite Kit too. And if anything interesting or noteworthy comes up, I’ll document it here as well.