Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting custom font in objective-c

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!

like image 620
Vancore Avatar asked Dec 05 '22 20:12

Vancore


1 Answers

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

like image 199
Roberto Ferraz Avatar answered Dec 25 '22 19:12

Roberto Ferraz