Add a custom font to iOS

Source: Internet
Author: User

Get ready:
Font file (. ttf,.odf)


Method 1:
Add the corresponding font (. ttf or. odf) to the resurce of the project, use the Fontlabel library in cocos2d, Fontlabel inherit the Uilabel, and use it like a Uilabel
FontName directly using the added resource name


Method 2:
1. Add the corresponding font (. ttf or. odf) to the resurce of the project, such as My.ttf
2. Add a Fonts provided by application in Info.plist (item0 corresponding value is My.ttf, add multiple fonts to add it in turn)
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, Mac under the title bar of the window under the font family name,windows can display family name and font name, the following sentence with family name and font name can be
Self.labelTest1.font = [Uifont fontwithname:@ "RETURN to CASTLE" size:30];


You can also traverse all fonts in the following ways:
Here is the code snippet:
Stupid way: Print out the fonts, look for non-system fonts ... (A very non-language 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];

In the program to first add this code, run, view console, the above program will list all the fonts, of course, also contains the font added by uiappfonts, but please note that the name may be very large gap, to find their own, not the font of the file name, the mistake will not see the effect.

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.