Custom fonts aren't working - objective-c

That we can use a custom font within my app is not a secret, but I can't reach this and wonder why.
Of course I searched the whole web for this problem but nowhere found something similar, so this is why I finally need to ask this.
What I'm trying: I'm on my way to create a small game and draw always my whole gamefield. Good, this works just fine with the default fonts, but doesn't with my custom font...
I added my .ttf-Font (I tried two different) to my project using drag and drop and copy it to my project folder. Next I added MyFontXY.ttf to my Info.plist using UIAppFonts and an array.
Next I tried to use it with [UIFont fontWithName:#"MyFontXY.ttf" size:12] - ok fine, doesn't work. I searched for the real name of the font and tried [UIFont fontWithName:#"My Font XY" size12] but it doesn't work, too.
So I tried several things after this like cleaning, reimport, change the name, ... but nothing happened. I also used NSLog(#"%#",[UIFont fontFamilies]) to find out which fonts are available, but my font wasn't there.
I also checked if the numbers were available with this font and they are, so I excluded a lot of possibilities now.
What can I do now? Does someone has this problem too?

You need to add the fonts to your target, for instance by manually adding them in the "Copy Bundle Resources" phase of your target

Related

Xcode: all .h files lost syntax coloring

I had a project that was working for almost a week then suddenly all my .h files lost code completion/syntax coloring.The only changes i could think of was i changed my target's name and then when i created a new set of .h and .m files this started.
Okay I have checked everything on this topic which was commonly an issue in xcode 3 and 4, i tried all solutions like
Clean/rebuild/restart Xcode
Remove Derived Data
Precompile Prefix Header to No
Add "$(SRCROOT)/**" to header search path
Nothing has worked, any help would be appreciated.Also my Xcode version is 6.1.1.
Try these Solutions:-
You can able to fix that issue by change build settings like this, PreCompile Prefix Headers :NO
I've been doing this, and it's worked multiple times for me (after trying all of the above previously).
Now I just hit space, backspace (the mac version) and rebuild... works nice. Then, remember to scroll (sometimes the colors don't show up until you scroll somewhere)
Find your prefix file: "ProjectName_prefix.pch".
Comment out some line. (basically change it)
Build your project, doesn't matter if it fails or not.
Uncomment it.
Build again.
I'm betting only step 2 (modify the prefix) is what does it, but these essentially get you back to running. Suddenly everything magically recolors itself and completes functions.
Good luck if that doesn't fix it, perhaps try doing this to your dependency pch files (three20 or FB api's)
Okay I found out what the problem was, in my case i had changed the target name and in the process the project name also.
This resulted in the change of path of the prefix header when actually the file was still at the old path. Changing it to the old path fixed the problem.
The prefix header path can be found in Build settings.
usually something like ProjectName/Prefixfilename.pch

Icons, Asset Catalog and Info.plist confusion

I have an app that started life for iOS 5 and has been updated ever since. For iOS 7 I have switched over to using an Asset Catalog for all the resources, which is nice and appears to work well. However, when I try to submit to Apple I get validation errors:
Invalid Image Path - No image found at the path referenced under key 'CFBundleIconFiles': 'Icon#2x.png'
I get the same error for the other icons, too. These icons do appear in the Asset Catalog and the names -- right down to the case -- match exactly as far as I can tell. The Asset Catalog is in the "Copy Bundle Resources" and all the resources appear in the right place when I run the app on both my iPhone and iPad (in iOS 7) and in the Simulator for iOS 6.
I have updated the app to use the minimum deployment target of iOS 6.
So, how do I successfully submit my update to Apple? Do I need the references in the Info.plist? Are there any other settings that I should check? Is the warning spurious and something that I should ignore (after raising a Radar)?
When an asset catalog is compiled for iOS 6 and lower, the images are put in the root of the app bundle, as if you were just copying them the old way. When this happens, Apple names the images according to the asset name, rather than the filename, which means we can still use +[UIImage imageNamed:] to get the assets on iOS 6 and lower.
This is true for the App Icon asset, when we have the following icons set in our asset catalog:
They become compiled like so:
As iOS 6 is still iOS 6, these files must be referenced in the Info.plist, otherwise the existing system wouldn't work. Looking in the compiled Info.plist for this test app, you see that Xcode has added CFBundleIconFiles for us and so we don't need to.
I have uploaded the test project I used to github.com/danielctull-tests/AssetTest.
Okay, so here is what I ended up doing. I'm not 100% sure it's correct but I thought that it was worth sharing.
I removed CFBundleIconFile, which I don't think is used in iOS 6 and above
I used the asset name rather than the icon filename in CFBundleIconFiles
I'm not clear what the proper names are so, for the sake of clarity: by icon filename I mean the name visible in the Attribute Inspector of the Asset Catalog when the icon is selected; and by asset name I mean AppIcon, which refers to seven actual icons in my case.
This both passes Apple's validation step and appears to look okay. I don't currently have an iOS 6 device but it displays correctly in the Simulator.
I had also a lot of Problems on this matter - I was constantly getting the error with the missing CFBundleIconName and that my Icons where not found. So here is my story, maybe it will be of help for somebody. By the way, I am using Visual Studio with Xamarin.
Add an Asset Catalogue to your Project with AppIcons (the actual name of the asset is not important but just that it is for the application icons). Add all of the necessary icon sizes. For the 'App Store' icon I have added an icon without a # in the name as someone in the forums suggested, but I am not sure, if it is 100% necessary. For generating the different icon sizes there are a lot of Websites and tools that can do that for you and you just need to provide the 1024x1024 one. They will generate the rest.
In the Info.plist under 'Visual Assets' and then 'App Icons' set the source to the asset that you have just created.
Now check your Info.plist. Do not open it with an external Editor, because all of your changes will be overwritten once you build your project. Do the following - right mouse click on the Info.plist in the Solution Explorer then select Open With -> Generic PList Editor.
Check that you have the following entries:
-Property = CFBundleIconName, Type = String. Value = Assets.xcassets/AppIcons.appiconset
-Property = XSAppIconAssets, Type = String. Value = Assets.xcassets/AppIcons.appiconset
Note that Visual Studio automatically adds, when it adds something altogether, 'Resources/Assets.xcassets/AppIcons.appiconset' as the Value. But in my case the Asset Catalog was created outside of the Resources folder and therefore, my icons where not found. So, check where your assets folder was created.
CFBundleIconFiles was not needed, because Apple uses the Asset Catalog instead now.
I hope that I was of some help :)

How to intercept reading of plist values in Objective-C code?

We're using the new Urban Airship iOS plugin for PhoneGap.
In the plugin's plist file, we're supposed to enter the app-specific keys needed to enable push notifications.
The problem is we have two versions, free and paid, of the same app, but the plist file only accommodates one version.
Essentially, we need to modify the Objective-C code to read different plist values, depending on whether it's the free or premium version.
We currently manage both versions with the same code base and Xcode project. Unless we change the plugin code, it seems like we need to create a new Xcode project, which we don't want to do.
How do we adjust Urban Airship's Objective-C files to read different values from the plsit file?
Sorry to keep you waiting, I wanted to give you a very detailed answer instead of rushing last night :) So here we go.
First in your project we need to add a new target. Go to your project settings and right click your target. Click duplicate.
You'll get a new target probably named Target-copy. You'll also get a new info.plist file just for that target.
Next we're going to edit our Pro version's Built Settings. Scroll or search and find Apple LLVM compiler 4.0 Preprocessing. Add to both your Debug and Release configurations. I normally just go with the simple PRO=1. You also need to add PRO=0 to your lite version or it will be undefined when you try to build that version.
Now lets look at how to add a custom plist like I'm sure you'll need. First create two folders. Its important these are folders not groups. In each folder we can create a plist with the exact same filename.
Since Now you can add something to each of them. I just added a key property and a value pro string / lite string. Finally to the code. In the sample project I made I simple overrode viewDidLoad but obviously this will work anywhere. Since the plists have the same name you can load them with one line of code. They'll never get mixed up because they are only copied to their respective target. If you need to do code level based logic you can use the PRO preprocessor we made.
- (void)viewDidLoad
{
[super viewDidLoad];
// This will load the proper plist automatically.
NSLog(#"Plist Value: %#",[[NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"Property List" ofType:#"plist"]] objectForKey:#"property"]);
// Also remember we set up a preprocessor PRO. you can use it as well.
if (PRO) {
NSLog(#"Only Show for Pro");
} else {
NSLog(#"Only Show for Lite");
}
NSLog(#"This will show for both");
}
This is the method I use for all my lite/pro version apps so I can share a common codebase without copying it between projects or other complicated systems. It has worked pretty well for me so far :) Happy Coding!
Source
Figured someone may be able to use the project to look at so here it is on GitHub.

Multi language localization storyboard support issue

I have an issue with my project storyboard. I have added two localizations more to storyboard, so now I have three storyboards: spanish, english and basque. All of them depending from the main one:
Now I have the issue that I have to update all changes manually to all to storyboards, because if I do changes to MainStoryboard, the others are not updated with this changes.
How can I modify all storyboards at the same time?
Thanks
Here is a really great tutorial for automating changes from one storyboard localization to all the others:
http://danielsaidi.wordpress.com/2012/08/20/localization-in-ios/
http://www.youtube.com/watch?v=cF1Rf02QvZQ
From what I gather, the solution involves adding a python script to the end of the build that syncs the storyboards and generates string files.
Go to the links for a more detailed solution. They said it better than I can.
You could either take out all the strings from Storyboard and put them in localized set of strings. Then you would have to do everything in your code like this:
self.label = NSLocalizedString("LABEL_STRING", nil);
or you could open the Storyboard as an XML file and do it there maybe a little faster. But other than that, no it is not doable, bcs the Storyboard localization serves you for polishing your localization. Say you have a screen called "HOME" and then (using google translate), you have "HASIERA" in basque. The problem here is, that in basque it's longer, so you need to adjust the GUI elements so it fits the screen perfectly. What could happen is that the string would be cut off, which we don't want to happen. That's why you need to do Storyboard localization step by step. If you don't need it, use strings file instead...

Custom fonts on iOS app - working in Simulator but not iPad

Encountering a weird problem here. I'm developing a game for my school project (non-commercial), and I'm using a custom font Black Chancery (free under GNU GPL). I followed the instructions from multiple sources, which includes:
Add the font to the project (TTF).
Modify app-Info.plist to add the font to it ("Fonts provided by application").
Using [UIFont fontWithName:#"BlackChancery" size:30] when the font is needed.
I could get the font displayed in the Simulator, however when I load it into my iPad, the default system font is used. I'm pretty sure there isn't a problem with the font itself as it displays in the simulator, and I've used FontForge to open the font without any warnings (following from This Question).
Any help is greatly appreciated. Thanks! :)
I can only guess as you haven't posted the contents of your plist or a directory listing of the bundle, but many cases of "resource works on the simulator but not on the device" are caused by the fact that the OS X filesystem is normally configured to be case-insensitive while the filesystem on the device is case sensitive. For example, if your file is named "BlackChancery.TTF" and your plist refers to it as "BlackChancery.ttf", it will be found on the simulator but not on the device.
I was having problem with font not recognizing, I fixed it by checking the correct name of the font by checking info of the font file by Get Info option. In my case the file name was written xyzfont.ttf but actually it was XyzFont.TTF in the info, i replaced and it worked.
Hope, it helps someone.
Another Way
I have come across one more way of finding the correct name, is by installing the font in the FontBook..
Just open FontBook from Finder and select User now from File->Add Fonts select the font you want to add into your application, after little processing the FontBook will show the Font listed in with the Correct name, use the name in the FontBook ignoring the actual ttf file name you have imported or, added to plist.. It should work..
I had the same problem which was resolved with a slight variation on iphonc's solution. The case sensitivity was directly related to the file extension. The problem was associated with my font file named: Choc.TTF
I had to remove the reference to the file in xCode 4.1
Rename the file to Choc.ttf (note lower case file extension)
Add the reference back into xCode
Perform a clean and re-build for the device
Conclusion (in my particular case):
Case sensitivity applies not JUST to the file name, but to the file extension as well (i.e. iOS device appears to tolerate only lower case).
My answer is different from all the rest. I had a problem because the font was all one word and lowercase "compassnormal.ttf" and the name in the file was Compass. So, my code was:
[UIFont fontWithName:#"Compass" size:24]]
Bundle Resource said:
compassnormal.ttf
~info.plist said:
compassnormal.ttf
None of this worked until I changed the actual filename to match it's official name in fontbook.
deleted all references from Bundle Resources and ~info.plist;
added font with updated name to Bundle Resources;
updated plist with new name;
tested in simulator and on device, Voila!
I have also experienced a problem with fonts containing the dash (-) character. Remove that character from your font names and try with that.
So your font named Gotham-Black.ttf should be named GothamBlack.ttf
Also check that your fonts are not zero bytes. I had this same issue and it turned out that my font files had emptied themselves at some stage. Probably when rearranging them in XCode and AppCode.
You have to use the real font name in the [UIFont fontWithName:#"... method! Not the ttf filename!!!
This real name is mostly far away from the filename. Just open the rtf in the Mac font utility. In the header you see the font family!!! Only the family!!! if you now use
NSArray *fontNames = [UIFont fontNamesForFamilyName:#"MyFontNameFamily"];
NSLog(#"%#", fontNames);
in your code, you get the real real real name in the console ;-)
But in the plist entry you still need the (case sensitive) filename!!!
I have the same issue on Xcode 6. My file name was My Font.ttf, it doesn't work. I manage to make it works when I rename it to My Font.TTF, just change the file extension to uppercase.