简体   繁体   中英

How to use a .ttc custom font in iOS

I read that iOS supports .ttc files (font collection) but i don't know how to specify the different font weights with only one file.

I am used to doing it by file name like this

[UIFont fontWithName:@"ProximaNova-Bold" size:17.0f];

but with a .ttc file i have only one file.

Adam's guidance is correct but incomplete if your question is about adding a font not provided by the system.

  1. Add the font to app bundle and make sure that it is part of the 'Copy Bundle Resources' phase. You can also select the font in the project navigator and then check for large membership in the file inspector on the right.
  2. Open your app's plist and add the font's file name in an array for the key UIAppFonts 添加<code> UIAppFonts </ code>
  3. Get the font's postscript name (as Adam instructed)

    1. Install the font on an OS X machine
    2. Open the font in Font Book
    3. Open the font family and then the font
    4. View > Show Font Info. Note the "PostScript name".
  4. Reference the font in your code using the PostScript name [UIFont fontWithName:@"AppleSymbols" size:24]

You say that you are "used to doing it by file name", however, that isn't how fontWithName:size: works. The "name" field in this case should be the name of the font itself. If it worked in the past with the name of the file, that's only because you got lucky and the name of the file and the font happened to be the same.

There are two ways to find the name of the font:

1) Install the font on an OS X machine, and then, in Font Book, open the font, click on the family you want, and use Preview > Show Font Info. You'll want the "PostScript name".

2) With the app running, stop in the debugger and call [UIFont familyNames]. Find the likely candidate for your font among those names. Then call [UIFont fontNamesForFamilyName:@"Foo"] with the name of your family. Choose the font name that likely corresponds to your font.

Note that this solves the problem of "how to specify different font weights". The answer is that you actually load individual fonts out of the TTC by referencing them by name.

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