Follow

Embedding Fonts

Note: It is your responsibility to ensure you have a valid licence for the fonts you embed.

Adding embedded fonts to your project

1) Create a new directory in your project folder via Finder: i.e. “Fonts”

2) Copy the fonts you wish to embed into this folder.

3) In Xcode right click on the group you wish to add your “Fonts” folder to and select “Add files to <group name>”. Select radio button “create groups from added folders” and click “add”.

4) Under supporting files. open the file: <project name>.plist

5) Select any of the keys in the plist file and press the ”+” button to add a new key. Add a new key named “Fonts provided by application” (array). Click the disclosure triangle to add fonts to your array. Under the value column of each item in the array, type the name of the font including the file's extension.

Using embedded fonts in your markup

You can now define the font in CSS in the usual way. For example,

@font-face {
    font-family: 'MyFont Sans';
    src: local('MyFontSans'), url('fonts/MyFont-Sans.otf') format('opentype');
    font-weight: normal;
    font-style: normal;
}

The local definition in the font must match the font name that you have embedded in your app. Note that this doesn't necessarily match the filename - it will be the name you would use to load the font in iOS using UIFont's fontWithName call. If you are unsure of your font's name, you can use some helper code to find the font family:

NSArray *fn = [UIFont familyNames];
NSLog(@"Families: %@", fn);

and then find the actual font name by listing all fonts from that family:

NSArray *fn2 = [UIFont fontNamesForFamilyName:@"NameOfYourFontFamily"];
NSLog(@"Fonts: %@", fn2);

Once the font face has been defined, it can be used in the normal way. For example,

p { font-family:'MyFont Sans' }

Using embedded fonts in Objective-C

You can also use the font in a native object. This can only be achieved programmatically as Xcode does not allow you to access custom fonts from within the Interface Builder.

For example, this is how you would set the font family along with the size on a UILabel.

[yourLabelName setFont:[UIFont fontWithName:@"MyFontSans" size:18]];
Was this article helpful?
0 out of 0 found this helpful
Have more questions? Submit a request

Comments

Powered by Zendesk