How to add a custom font in iOS

Source: Internet
Author: User

Apple has a lot of work to do with development, but no matter how much effort there is on a platform, there are still some requirements that cannot be covered, such as fonts. In order to be more personalized, we will need different fonts, sometimes some fonts are very special, even buy, then how to add these fonts to the project?

Font has the corresponding font file, usually with TTF or OTF suffix, we can go to some third-party website to download (also someone specifically made a font for sale), Google fonts is a better site, you can look for it. Let's use the open sans font as an example. First of all, of course, to download the font file, downloaded after the discovery is 10 TTF files, yes, this is a series of fonts, there are bold, italic, there are different sizes, convenient for users in different scenarios to use.


After you have prepared the font file, you can add to the project, is to add TTF files to the project, it is important to note that the TTF is added to the bundle, that is to say, the final IPA file must contain these TTF (otherwise where to find the font in the application?). )

After joining, we can see them in fileexplore and build phase, as shown in the following:

The final step here is to add the appropriate information in the Info.plist file, which actually adds fonts provided by application to the info page of the project, and sets the corresponding TTF file in, which tells the application, I want to add a new font, which is the corresponding font file.

Here, the font file should be ready, the following we need to confirm the name of the font, because the code needs to use the name to specify fonts. Generally speaking, the name and file name are the same, but there are different times, so this step must not be ignored.

The name of the font here is obtained by nslog This print statement, we look at the code:

    For (NSString *fontfamilyname in [Uifont Familynames]) {        NSLog (@ "---%@---", fontfamilyname);        For (NSString *fontname in [Uifont Fontnamesforfamilyname:fontfamilyname]) {            NSLog (@ "  %@", fontname);        }                NSLog (@ "   ");    }

This code we read the name of the font family one by one, and then each of the families reads the name of each font. We can tell from the code that we actually print all the fonts, all of them are too long, so we've got a section listed and our Open sans.

Let's look at some fonts with the same name and file name, such as Opensans-bold, and some font names and filenames, such as opensanslight-italic. At this point, the preparation work has finally been completed.

The preparation work is quite cumbersome, each step can not save, but the following formal use is very simple, I made three labels, respectively put in three fonts, in order to salute the master, all show Hello world! The words.

The code is this:

    Self.label1.font = [Uifont fontwithname:@ "Opensans-bold" size:20.0];    Self.label1.text = @ "Hello world!";    Self.label2.font = [Uifont fontwithname:@ "Opensans-bolditalic" size:20.0];    Self.label2.text = @ "Hello world!";    Self.label3.font = [Uifont fontwithname:@ "Opensans-light" size:20.0];    Self.label3.text = @ "Hello world!";

The effect is this:

In this way, we are in the application of the perfect addition to the custom font, the application will appear more personalized, let a person in front of the bright, stand out!


How to add a custom font in iOS

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.