4

Hiho,

I am trying to set a font for my label, but it just doesn't appear in my iOS-Simulator when I run the app.

_textLabel1 = [[UILabel alloc] initWithFrame:CGRectMake((CGFloat) (self.view.center.x - (self.view.bounds.size.width / textWidth / 2)),
            (CGFloat) (self.view.center.y * textPositionY), (CGFloat) (self.view.bounds.size.width / textWidth), (CGFloat) textHeight)];
_textLabel1.numberOfLines = 0;
_textLabel1.textAlignment = NSTextAlignmentCenter;
_textLabel1.textColor = clr;
_textLabel1.text = @"Favoriten";
[_textLabel1 setFont:[UIFont fontWithName:@"assets/fonts/Kingthings_Trypewriter_2.ttf" size:36]];
[_favouriteViewer addSubview:_textLabel1];
_textLabel1.font=[_textLabel1.font fontWithSize:25];

I also added the custom font in my plist:

<key>UIAppFonts</key>
<array>
    <string>Kingthings_Trypewriter_2.ttf</string>
</array>

I saved my custom font in the named asset folder wich I called in:

[_textLabel1 setFont:[UIFont fontWithName:@"assets/fonts/Kingthings_Trypewriter_2.ttf" size:36]];

I does not show the font in the simulator.

Thanks for the help!

Edit: I am using AppCode, not Xcode.

Edit2:

Thanks to Ganesh Somani, I used his code to try to find out if my font is correctly added to the project via the Copy Bundle ressources but unfortunately it is still not showing up in the log.

for(NSString *fontfamilyname in [UIFont familyNames])
{
    NSLog(@"Family:'%@'",fontfamilyname);
    for(NSString *fontName in [UIFont fontNamesForFamilyName:fontfamilyname])
{
    NSLog(@"\tfont:'%@'",fontName);
}
NSLog(@"---");
}

Solution: For some reason I had two .plist files in my project. I put in the following code and now it works:

<key>UIAppFonts</key>
<array>
    <string>"fontName.ttf"</string>
</array>

Thanks to all contributers for the help!

Vancore
  • 657
  • 1
  • 9
  • 18
  • did you try using just `[UIFont fontWithName:@"Kingthings_Trypewriter_2" size:36]` – Vin Sep 15 '15 at 08:01
  • 1
    first thing first. what is your ___font name___? that is rarely equal to the precise file's name because it is definitely not the file's name with the path... so probably you need to find the ___font name___ first. – holex Sep 15 '15 at 08:23
  • @Vancore i have answered same question in my [previous post](http://stackoverflow.com/questions/15824675/having-trouble-installing-specific-font-in-an-ios-app-incompatible-ttf/15825944#15825944). Please check it, it will surely help you. – Dipen Panchasara Sep 15 '15 at 08:32
  • @Vin I did, but this does not seem to work. – Vancore Sep 15 '15 at 11:26
  • @holex How can i find out the font name? I tried this, but "Kingsthings" won't show up: for (NSString *fontfamilyname in [UIFont familyNames]) { NSLog(@"Family:'%@'", fontfamilyname); for (NSString *fontName in [UIFont fontNamesForFamilyName:fontfamilyname]) { NSLog(@"\tfont:'%@'", fontName); } NSLog(@"~~~~~~~~"); } – Vancore Sep 15 '15 at 11:27
  • @Vancore, one of the answer has recommended already to use _Fontbook_ app, I would recommend the same to figure out because that will show you the ___font name___ on the font's info screen. – holex Sep 16 '15 at 08:04
  • @holex I used the Fontbook and found out about the PostScript name which is required. Sadly I still can't seem to find a way to display my font. – Vancore Sep 16 '15 at 08:58
  • @Vancore, you could share the font maybe to take a closer look on that... because the entire procedure is described precisely in answers. – holex Sep 16 '15 at 09:31
  • @holex http://www.fontsquirrel.com/fonts/Kingthings-Trypewriter This is the font I want to use. Maybe there is a problem with the font itself. – Vancore Sep 16 '15 at 11:13
  • I'm a genius ... for some reason I had 2 .plist files in my folder. Now I added UIAppFonts Kingthings_Trypewriter_2.ttf to the file and it works. Thank you very much for the help! – Vancore Sep 16 '15 at 11:37

4 Answers4

14

Just import your font files to your project:

import files

Then add them to your Info.plist file:

plist file

Then you can use them in your code:

enter image description here

Make sure you use the correct name, in the font application (after clicking twice and installing your font in your computer, you can see this information) tap CMD + i, and check the "PostScript name" of your font, that is how you will call it.

enter image description here

Open your project on Xcode, tap on the project icon, select the target, then Build Phases, go to the section Copy bundle resources and check if your fonts are there (remember that you need to add them to your project folder):

enter image description here

Roberto Ferraz
  • 2,511
  • 24
  • 43
  • Thanks for the reply! I forgot to mention that I only use AppCode, I am not really into XCode. My Font-Menu (I am relatively new to Mac) looks a bit different, I cant find the PostScript name for my font. I will try to add a screenshot. – Vancore Sep 15 '15 at 11:39
  • I opened my project in Xcode and it seems to added in my plist file: http://www.directupload.net/file/d/4111/plifkr8l_png.htm – Vancore Sep 15 '15 at 11:52
  • Sorry for the spam, I now found out the real name ... KingthingsTrypewriter2. But still does not work. [_textLabel1 setFont:[UIFont fontWithName:@"KingthingsTrypewriter2" size:36]]; _textLabel1.font = [UIFont fontWithName:@"KingthingsTrypewriter2" size:36]; – Vancore Sep 15 '15 at 14:31
  • Did you add the Font to your project folder? Try to delete the app of the simulator, clean the build and install again.. also see the last EDIT I made – Roberto Ferraz Sep 16 '15 at 07:22
  • I added the font as you can see in this picture: http://www.directupload.net/file/d/4112/sik6j58p_png.htm So its in my folder + in my plist file and still doesn't work. – Vancore Sep 16 '15 at 08:50
  • After doing all this.. did you try to use the method from Ganesh again? the one from the other answer.. it should give you some result now – Roberto Ferraz Sep 16 '15 at 09:02
  • I tried, but my font still is not shown in the log. – Vancore Sep 16 '15 at 09:06
2

Custom Fonts can be headache at times.

I once had the same issue but with labels.

This answer solved it for me. Maybe this helps you.

Also Found a very nice article on how to add custom fonts to iOS app

Edit

The other possibility is that you are not using the correct Font name, but using the file name

Use the following code to find the real name

for(NSString *fontfamilyname in [UIFont familyNames])
{
    NSLog(@"Family:'%@'",fontfamilyname);
    for(NSString *fontName in [UIFont fontNamesForFamilyName:fontfamilyname])
    {
        NSLog(@"\tfont:'%@'",fontName);
    }
    NSLog(@"---");
}
Community
  • 1
  • 1
Ganesh Somani
  • 2,280
  • 2
  • 28
  • 37
  • Answers only contain links might not good enough. Better to explain what OP missed or just make comments. – WangYudong Sep 15 '15 at 08:12
  • @GaneshSomani That's what I tried as well, but my custom font was not in the list. What can I do about this? – Vancore Sep 15 '15 at 11:29
  • Did you check "Build Phases"->"Copy Bundle Ressources"? – Ganesh Somani Sep 15 '15 at 11:42
  • Do you mean this: http://www.directupload.net/file/d/4111/plifkr8l_png.htm Thanks a lot for the replies! Sorry I forgot to mention that I use AppCode. – Vancore Sep 15 '15 at 11:54
  • I am sorry but I don't know exactly what is the interface for AppCode like. But it should be the place where you set flags for non-arc code... i'll add a screenshot whenever i get time – Ganesh Somani Sep 17 '15 at 03:07
0

Incase if you are using Objective-c, then to display the font families you can use the below loop:

for(int i =0; i < UIFont.familyNames.count; i++){
    NSString *myFontName = [UIFont.familyNames objectAtIndex:i];
    NSLog(@"here myFontFamily is %@",myFontName);
    for (int j = 0; j < [UIFont fontNamesForFamilyName:myFontName].count; j++){
        NSLog(@" here the font is %@",[[UIFont fontNamesForFamilyName:myFontName]objectAtIndex:j]);
    }
} 
executable
  • 3,365
  • 6
  • 24
  • 52
Jeet Kapadia
  • 31
  • 1
  • 7
0

In addition to the well explained @Roberto answer, if you're still not getting your fonts, Just use or assign your font to any Label in one of your Storyboard, You will start getting it using this code

If you still unable to get font using [UIFont fontWithName:@"Font-Regular" size:11]; There must be problem in naming. Stop at any Breakpoint in debugger and Debug it using different names, and see which po prints object that is not nil

po [UIFont fontWithName:@"FontRegular" size:11];

or

po [UIFont fontWithName:@"Font Regular" size:11];

or

po [UIFont fontWithName:@"Font-Regular" size:11];

You will get <UICTFont: 0x7fe2044021d0> font-family: "Interstate-Regular"; font-weight: normal; font-style: normal; font-size: 11.00pt for actual font name otherwise you will get nil

Baig
  • 4,737
  • 1
  • 32
  • 47