How to include and use new fonts in iPhone SDK?

2019-01-01 09:15发布

问题:

I want to use font \"MgOpen Modata\" in my iphone App. But I dont see it in the font list in Property inspector.

How do I include that font so that I can use it?

回答1:

  1. Add the font files to your resource files
  2. Edit your Info.plist: Add a new entry with the key Fonts provided by application.
  3. For each of your files, add the file name to this array

On the example below, I\'ve added the font \"DejaVu Sans Mono\":

\"Info.plist:

In your application you can the use [UIFont fontWithName:@\"DejaVuSansMono-Bold\" size:14.f].

Or if you want to use it in html/css, just use font-family:DejaVu Sans Mono;

Note: this is available in iOS 3.2 and later.



回答2:

I found this walkthrough was the best

Although the above accepted answer does work I found that if you added text into an XIB file and then set the font to our new font then the text didn\'t update.

Making sure the new font is included in resources fixed this problem (taken directly from walkthrough):

\"This should not be a problem but sometimes when you’re having trouble getting your font face to show up, this can be a source of headache so let’s double check now to rule it out as a potential pitfall.

Go to your project Build Phases pane by highlighting the XCode project file in your solution explorer and on the right hand side, select “Build Phases”. You’ll see that one of the sections you can expand is “Copy Bundle Resources”. Open that list and make sure that your fonts are included in that list.\"

\"Make

The other stuff in the walkthrough is the same kind of stuff (add the file to your project and to the plist file too)

All credit to Code with Chris for this answer which saved me a lot of time and effort and I am merely copying a shot from his page to allow this answer to stand on its own feet instead of requiring you to click the link.



回答3:

Don\'t forget to add the fonts to the targets compile bundle resources by going to \'Targets\' -> \'Build Phases\' -> \'Copy Bundle Resources\' and add all the fonts manually



回答4:

Most of the time we forget to add font to the application Target. Make sure you did not forget this important step. Mind checking right panel File Inspector > Target Membership checkbox.

\"enter

Also you can check wether font is added well to your application or not by running this code snippet in anywhere on your app.

for (NSString* family in [UIFont familyNames])
{
    NSLog(@\"%@\", family);

    for (NSString* name in [UIFont fontNamesForFamilyName: family])
    {
        NSLog(@\"  %@\", name);
    }
}

EDIT

ALPHABETIC ORDER:

for (NSString* family in [[UIFont familyNames] sortedArrayUsingSelector:@selector(compare:)])
{
    NSLog(@\"%@\", family);

    for (NSString* name in [[UIFont fontNamesForFamilyName:family] sortedArrayUsingSelector:@selector(compare:)])
    {
        NSLog(@\"  %@\", name);
    }
}

This will populate a list of available fonts in your app, where your added font will also appear.



回答5:

I think is better for a dev to have the plist structure as raw source code. I\'m leaving it for copy/paste:

<key>UIAppFonts</key>
    <array>
        <string>Roboto-BoldItalic.ttf</string>
        <string>Roboto-Medium.ttf</string>
        <string>Roboto-MediumItalic.ttf</string>
        <string>Roboto-Regular.ttf</string>
        <string>Roboto-Bold.ttf</string>
    </array>

Copy this snippet and modify the strings keys into the array to your desired fonts. Hint: To view the plist as raw source code please click secondary button over the file and select Open As -> Source code.

EDIT

// Logs fonts ordered by name
    for (NSString* family in [[UIFont familyNames] sortedArrayUsingSelector:@selector(compare:)])
    {
        NSLog(@\"%@\", family);

        for (NSString* name in [[UIFont fontNamesForFamilyName:family] sortedArrayUsingSelector:@selector(compare:)])
        {
            NSLog(@\"  %@\", name);
        }
    }

After adding the fonts could be useful to add to (for example) the AppDelegate.m class to see what are the real fonts names (on the bundle). A problem I encountered various times that I was loading the fonts with a wrong name assuming that the font filename as the correct one. Sometimes it differs from the bundle real name. In addition to this you can check the file full name using the file info window (cmd+i) on finder (macOS).

Example to clarify:
File Name = \"DroidSansEthiopic-Regular\"
Processed Name in bundle = \"Droid Sans Ethiopic\"



回答6:

In Xamarin iOS

Step 1: First of all you have to add your .ttf or .otf file into your Project.

like Roboto-BoldItalic.ttf .

Step 2 : Right click on the Font and select property

  1. Build Action --> Content
  2. Copy to output directory --> Always Copy

Step 3: go to info.plist

select property Fonts provided by application and add the font name.

\"enter

Step 4:

set Font programatically below way

account.Font = UIFont.FromName(\"Roboto-BoldItalic\", 18f);


回答7:

You can use [UIFont fontWithName:@\"ArialMT\" size:14]; method directly, if you want to change your font style. Also all the fonts may not be available in iphone library.Just refer this for available font styles.



回答8:

If you have .otf format. sometime this format doesnt work. Convert .otf format to .ttf. There are bunch of font converter online. Once you add the custom font to your .plist file under Fonts provided by application as explained above. Make sure in your Project Target under Build Phase -> copy Bundel Resourse that file is present. If not then add it. Then just in your code add this [UIFont fontWithName:@\"your custom font\" size:your choice of size];



回答9:

Its Easy and simple now- Tested in Xcode 9.2 and swift 4

Steps to add font to your Xcode

Select your UILabel, UITextField or whatever then under fonts section and follow

Step 1

Select settings menu from left corner of font selection screen. And choose font manager option.

\"enter

Step 2

Click on the add button as marked below.

\"enter

Step 3

Select the folder that contains your font. It will be loaded into XCode fonts list.

Steps to add fonts to your project

Don\'t forget to add description to your plist with the key Fonts provided by application. and put font files inside copy bundle resources under project target settings.

Yes! thats it.. njoy..