iPhone Development: how to use custom fonts? - objective-c

I used a special font in my photoshop design,
is it possible to use these fonts natively in the iphone app? or
do I need to convert them into an image first?
If this is the only way to do it, what do you do with dynamic text?

Copy your font file into Resources
In your application .plist create (if it's exist just create a row) a row called "Fonts provided by application" and then in "item 0" copy your font name for example "Ciutadella-Bold.otf" (if there are some spaces in font name, rename it and for example replace all spaces to minus '-')
Then you can define this font in your application:
UIFont *CiutadellaBold = [UIFont fontWithName:#"Ciutadella-Bold" size:17.0f];
And use in for instance in uiLabel:
[uiLabel setFont:CiutadellaBold];

UIFont *font = [UIFont fontWithName:#"MyFont" size:20];
[label setFont:font];
Where "MyFont" would be a TrueType or OpenType file in your project (sans the file extension), and label would be an instance of UILabel.

Yes it is possible with iOS 3.2 and later. You need to have the fonts file, I forget what formats exactly. Add the font file as a resource to your project then add the names of the fonts in the applications info.plist in an array under the raw key 'UIAppFonts' or under the friendly key 'Fonts provided by application'.
Here's a sample application that you can look at - https://files.me.com/tobiasoleary/ey08n1. It prints out the all the fonts accessible to the application. Two fonts have been added Flames and Firestarter.
This is by far the easiest way to add custom fonts to application. If you need to support custom fonts for iOS before 4.0 see http://github.com/zynga/FontLabel/tree/master
This question has been asked before here: Can I embed a custom font in an iPhone application?.

Note :
The parameter of “fontWithName” must be the real name of the font, not the name of the file. If you open your ttf file with the Mac Font Book, you will directly see its name on top of the window.

Related

Cannot change size of custom Font

Hi I am using below code to use custom font style for label text. I've installed it and work fine but the problem is that the size of text does not change. I've tried more times with different sizes. I don't know what should do
self.saveButton_label.text = NSLocalizedString(#"Saved", nil);
self.saveButton_label.font = [UIFont fontWithName:#"Helvetica Neue LT Pro" size: 13.0];
I believe the issue lies with one of the steps you need to take when adding a custom font to your project, you probably are not noticing the difference because Helvetica Neue is also the default font, which I believe is used if you were to assign nil to UILabel as the font.
I suggest following this guide, I've used it as a checklist myself when using custom fonts and it has worked, especially take not of the naming, the name you have to use in code might not be the one you see in FontBook or when using Preview on the font.
Try this code, In your code you are missing "-" with font name.
self.saveButton_label.text = #"Saved";
[self.saveButton_label setFont:[UIFont fontWithName:#"Helvetica-Neue-LT-Pro size:13.0f]];
also you can use below fonts.
HelveticaNeueLTPro-MdCn
HelveticaNeueLTPro-BdCn
Hope it will work for you.

Labels are not changing to custom font

I am trying to set a label to a custom font with the following code.
cell.lblDataum.font = [UIFont fontWithName:#"Homenaje_Regular" size:10.0];
I'v added this font in my .plist.
<key>UIAppFonts</key>
<array>
<string>Homenaje_Regular.ttf</string>
</array>
But still no succes. Can anybody help me?
Kind regards
Are you sure you added you Homenaje_Regular.ttf file to your Xcode project and added it to your target's Copy File Build Rule (checkbox is checked next to the target name in the Inspector Panel?) so that the TTF file is copied into the application bundle on compilation.
But more likely, the name of the font is probably not #"Homenaje_Regular". You have to use the real font name as a parameter for fontWithName:and that's not always the same as the file name.
In your case, the real name of your font is probably "Homenaje-Regular" or just "Homenaje" and not "Homenaje_Regular".
You can have the real name of the font by opening your font in FontBook on your Mac and simply press Cmd-I to get that information.
You can also list by code in your iOS app all the fonts available to make sure your font is correctly detected by your app and pick up the right name of your custom font to use with fontWithName: :
for ( NSString *familyName in [UIFont familyNames] )
{
NSLog(#"=== Family %# ===", familyName);
NSLog(#"Names = %#", [UIFont fontNamesForFamilyName:familyName]);
}
I believe, the name of the font is Homenaje-Regular, not the file name.

Custom font doesn't work

I'm trying to add a font into my project to use in a label but it just don't work. Here is what I'm doing:
I copy the font file into my project resources.
Set my plist file with the array key UIAppFonts.
Add my font's name Kingashandwriting.ttf (the real name with the extension
, not the file name) into the array.
Set up my font in the code: UIFont *font= [UIFont fontWithName:#"Kingashandwriting" size:15];
Set it into my label: [self.myLabel setFont:font];
myLabel is an IBOutlet that is linked with a label in interface builder.
I'm using Xcode 4.3.3
Am i missing something?
I remember that I ran into this same problem when implementing custom fonts into my application. I thought that I had the official name right but it was giving me problems.
My advice is to open up the file in font book and look at the top of the window and see what that name says. Then format it exactly how you see it in your fontWithName: method.
Here's an image of what you're looking for:
http://i.stack.imgur.com/uOZy6.png
Then your font method would look like this (based on the font in the image):
UIFont *font= [UIFont fontWithName:#"BankGothic Lt BT" size:15];
For further information, in my application my file is named bankgthl.ttf and I have that file name added into my plist array. But when I am setting the font in my code I call it the same as above using the name seen in font book and it is all working fine.

<Error>: FT_Open_Face failed: error 85

I am trying to install a custom font into my iPhone application. When I add the 'Fonts provided by application’ key into my info.plist file I get the following error ': FT_Open_Face failed: error 85.’
I am not sure what the problem is as I have successfully been able to use custom fonts in the past.
Any help will be appreciated.
If you leave the first element in your font array in the plist (index 0) blank, you will get this error.
PostScript fonts cannot be used with UIFont directly. You can use them only with CoreText or CATextLayer.
This can occur if the font in question is a Font Suitcase format. Open your application package, and if the font .ttf file shows a size of "Zero bytes", it's probably a font suitcase.
Files of this type can be converted using Fondu.
When running Fondu, I had to specify not just the suitcase file, but the resource fork inside it:
fondu mysuitcasefont.ttf/..namedfork/rsrc
This will extract any fonts in the suitcase to the current directory. It is these extracted files you will want to include in your iOS project and reference from the .plist.
I received this error when using a .ttf font. I was able to correct it by removing the file extension from the filename in (a) the resource in xcode and (b) the "Fonts provided by application" array in the .plist file.
Hopefully this will help.
I was trying to use Google Signika font on my App, and got the exact issue.
I solve the issue by using the .otf version of the font
Accepted answer is totally wrong and unnecessary. Probably you made a mistake when writing your font name.
Thats rules for that.
First Step : Write font name before .ttf extension. Example: For FFFTusj.ttf => FFFTusj
Second Step : Write - and after font style. Example: Bold or Medium etc. Example: #"FFFTusj-Bold"
And also dont forget add your font file to your project.
Check this link:
http://www.bycoder.com/post/2013/03/28/XCode-Custom-font-doesnt-work
If your font is REGULAR dont write -Regular at end of font name.

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.