Add custom fonts for iOS
Preparation:
Font file (. TTF,. ODF)
Method 1:
Add the corresponding font (. TTF or. ODF) to the resurce of the project and use the fontlabel library in cocos2d. fontlabel inherits from uilabel and can be used like uilabel.
Fontname: use the name of the added resource directly.
Method 2:
1. Add the corresponding font (. TTF or. ODF) to the resurce of the project, such as my. TTF.
2. In info. plist, add a fonts provided by application (the value corresponding to item0 is my. TTF, and add multiple fonts in sequence)
3. When using alabel. font = [uifont fontwithname: @ "XXX" Size: 30]; note that XXX is not necessarily my, here is return to Castle,
// Family name: Return to castle
// Font name: returntocastle
// Double-click. In Mac, the title bar of the window contains the font family name. In Windows, the family name and font name can be displayed. In the following example, both the family name and font name can be used.
Self. labeltest1.font = [uifont fontwithname: @ "return to Castle" Size: 30];
You can also traverse all fonts using the following method:
The following is a code snippet:
// Stupid method: print all fonts and find out non-system fonts... (Speechless method)
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];
Add this code in the program, run it, and view the console. The above program will list all fonts, including the fonts added by uiappfonts, But please note that the names may differ greatly, you need to find it by yourself. It is not the font file name. If you make a mistake, you will not be able to see the effect.