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!
Just import your font files to your project:
Then add them to your Info.plist file:
Then you can use them in your code:
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.
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):
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(@"---");
}
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]);
}
}
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
<UICTFont: 0x7fe2044021d0> font-family: "Interstate-Regular"; font-weight: normal; font-style: normal; font-size: 11.00pt
<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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.