简体   繁体   中英

Can't add custom font to application

I am having trouble using a custom font in my application.

The following describes the steps I took

1 - Add .TTF font to application
2 - Modify the info.plist file.
3 - Add the key "Fonts provided by application" to a new row
4 - And add each .TTF file (of font) to each line.

So I have an array of fonts, each of which has a .ttf extension.

And then I try to add the font

UILabel *lblName = [[UILabel alloc] initWithFrame:CGRectMake(25, 65, 230, 33)];
lblName.backgroundColor = [UIColor clearColor];
[lblName  setFont: [UIFont fontWithName:@"acmesab" size:32]]; // acmesab is my custom "font" name.
lblName.textAlignment = NSTextAlignmentLeft;
lblName.text = @"Name:";
[simapleView addSubview:lblName];

You should look at your font-properties what the actual font-name is. Sometimes you import the font with name FjallaOne-Regular . But if you look at the properties you see that the font is named like Fjalla One .

Therefore you need to use it like this.

[lblName  setFont: [UIFont fontWithName:@"Fjalla One" size:32]];

Instead of

[lblName  setFont: [UIFont fontWithName:@"FjallaOne-Regular" size:32]];

Also check if you set the correct target membership

You can see what I mean by the correct name over here

LINK

For the custom font first you have to install your .ttf file in your system.after that import it to your project. Rename .ttf file as shown in "FontBook" and add

 <key>UIAppFonts</key>
    <array>
    <string>Times New Roman.ttf</string>
</array>

in your info.plist file. and then use it as fontname in your project

CCLabelTTF *label1 = [CCLabelTTF labelWithString:@"Name" fontName:@"Times New Roman" fontSize:26];

Open your project -> Targeta -> Build Phase -> Copy Bundle Resources. Check your files are shows in list or not? If not then add them and run the application again.It should work.

Use following code to check whether font is added or not and add appropriate font name from listing.

NSArray *familyNames = [[NSArray alloc] initWithArray:[UIFont familyNames]];
NSArray *fontNames;
NSInteger indFamily, indFont;
for (indFamily=0; indFamily<[familyNames count]; ++indFamily)
{
    NSLog(@"Family name: %@", [familyNames objectAtIndex:indFamily]);
    fontNames = [[NSArray alloc] initWithArray:
                 [UIFont fontNamesForFamilyName:
                  [familyNames objectAtIndex:indFamily]]];
    for (indFont=0; indFont<[fontNames count]; ++indFont)
    {
        NSLog(@"    Font name: %@", [fontNames objectAtIndex:indFont]);
    }
    [fontNames release];
}
[familyNames release];

It can be tricky sometimes as certain fonts don't seem to be recognised. Have a look at my answer from a few months ago which might help you - https://stackoverflow.com/a/10170599/331854

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM