Add custom fonts for iOS

Source: Internet
Author: User
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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.