iOS development: Partial font parsing

Source: Internet
Author: User

First, iOS native font display

Select font in the label, and change font from system to custom, you can see 72 special fonts in family. These have very cool fonts, but all are only for English numerals, not valid for Chinese. Write a program that iterates through all the native styles to show that you can achieve the following results. You can clearly see each font corresponding to the style, do not have to try again.

Altogether is 72 kinds of styles, I this demo program has two kinds of demonstration method, the concise display and the detailed display, the concise demonstration only will take each family the first font to show. The last picture is a detailed display of the interface. Divided into group show, each section corresponds to a family. It can be seen that Apple's native fonts still have a lot of beautiful fonts, but only support in English.

Believe that most of the software now most of the project is so write code:

Label.font = [Uifont systemfontofsize:14];

If you do not want to use the default system font, you need to assign the value using this method:

Uifont *font = [Uifont fontwithname:@ "Georgia" size:14];

The argument in name here is Familyname rather than fontname.

 Second, get family name

So how do I get the name of this family?

Method 1: In storyboard or xib, use the graphical interface of the label to select a style that you like, and then write down the name in the code.

Method 2: There are above, from the above 5 pictures selected.

Method 3: (recommended) traversal

The Uifont class has these open APIs about family names and font names, which allow you to write a clear traversal print to see all the familyname and the fontname contained therein.

int i = 0;

For (NSString *fontfamilyname in [Uifont Familynames])

{

NSLog (@ "Family: '%@ '", fontfamilyname);

For (NSString *fontname in [Uifont Fontnamesforfamilyname:fontfamilyname])

{

NSLog (@ "Tfont: '%@ '", fontname);

}

NSLog (@ "-------------%d", i++);

}

Using the above code, you can iterate through all the names and then copy the names from the print to the code. Personal feeling is more scientific.

 Third, the external font introduction project

I test, in the Internet whether it is a Windows font, or Android fonts as long as the TTF format, the general iOS program support embedded.

The exact steps are also simple:

1. Drag the TTF file into the project

2. Modify plist file, add fonts provided by application configuration, and then fill in the name of the project dragged in

3. You can see the new font selection in the graphical interface

4. If you do not want to look from the IB interface, it is recommended to use the above traversal printing, you can use the circulation of the number of print to see whether the most direct import success, and find their own content.

5. Run the project get the results you want

  Four, dynamic font

Dynamic font-dynamic type originates from a text rendering framework Textkit introduced by IOS7. The main function is to allow the system to set its own size. The current Apple has done more and more humane processing, even the blind mode has. For the display of fonts also take into account the preferences of each person, some people like to see characters, some people like to see the small print. Before the dynamic font came out, some applications also took into account the user experience, such as NetEase news before there is the ability to set preferences in the application of the font size function. Apple is also integrated into the entire handset, the dynamic font of the idea is: in the setting font size, not only the system font will change, even the application of the font size will change. The premise is that the code for the fonts in your application is written in accordance with the requirements.

Most of the previous article is to choose Custom in the font, at this time to consider dynamic font has the choice of Text styles.

Uifonttextstyleheadline

Uifonttextstylebody

Uifonttextstylesubheadline

Uifonttextstylefootnote

UIFontTextStyleCaption1

UIFontTextStyleCaption2

These styles, as the name suggests, do not each have a display effect listed. Title, sub headings, text, etc. are some of the more simple. I feel like it's the "style" in Word, the title of each chapter of their own title selected set to title one or title two and then you can use Word to automatically generate the directory function, and a change in a style of detailed settings, each title subheadings will change the format. Here is if you use these styles in your code, you will be able to apply fonts after you have set the size in your phone setting.

Set the position of the font is: set-show and Brightness-"text size

As the top left figure is the size of the maximum after the effect, the following prompts only to the maximum will be displayed. I tried the QQ in the font settings are dynamic fonts, micro-letter and Alipay temporarily does not support dynamic size, in the font size set very large after the micro-letter Alipay did not respond, QQ response to change. Above the right picture is on the iphone6 to the QQ client display effect.

The code that sets the dynamic font is implemented as follows:

Uifont *font = [Uifont preferredfontfortextstyle:uifonttextstylesubheadline];

It is recommended that you combine dynamic fonts with automatic layouts to prevent misplaced bugs from changing font settings.

 Five, typeface descriptor

Font descriptor-uifontdescriptor is also one of the core of Textkit, the general meaning is: Font descriptor can be a you do not know the details of the font style temporarily save to make changes or assign value to others to use. After using the dynamic font above, you may only know the current Text-style but the detailed familyname,fontname do not know what, in this case, if you want to modify the font style is italic or bold, you can only use this method:

------Remove the font style for the current body

Uifontdescriptor *bodyfontdesciptor = [Uifontdescriptor preferredfontdescriptorwithtextstyle:uifonttextstylebody];

------Change the style to italic

Uifontdescriptor *italicfontdescriptor = [Bodyfontdesciptor fontdescriptorwithsymbolictraits: Uifontdescriptortraititalic];

------assignment to another label.

Self.titleLabel.font = [Uifont fontwithdescriptor:italicfontdescriptor size:0.0];

There are four types of options available for styles:

Uifontdescriptortraititalic

uifontdescriptortraitexpanded

Uifontdescriptortraitcondensed

Uifontdescriptortraitbold

The font descriptor also has an API that sets a label's style through the detailed property dictionary, as follows

Uifontdescriptor *attributefontdescriptor = [Uifontdescriptor fontdescriptorwithfontattributes:

@{uifontdescriptorfamilyattribute: @ "avenir Next Condensed",

uifontdescriptornameattribute:@ "Avenirnextcondensed-italic",

Uifontdescriptorsizeattribute: @40.0,

Uifontdescriptormatrixattribute:[nsvalue valuewithcgaffinetransform:cgaffinetransformmakerotation (M_1_PI*1.5)

]}];

Label.font = [Uifont fontwithdescriptor:attributefontdescriptor size:0.0];

This set up the family name, font name, size, deformation, The last size can be filled in 0.0, if a value is filled in, that value will be the size of the dictionary above, feel that the general situation should not be such as egg pain in this way to build style, this attribute according to command point into there are many, most of them are usually not used, interested can be a study, as if altogether There are more than 10.

The label created by the above code will display this:

Six, extended font style

It said that the original all do not support Chinese, but we use Chinese people or more, Chinese fonts are now online search results are many, but most of them are not the desired results, or will not let you so simple to download. I've sorted out a common font style package, which roughly includes:

Chinese Xingkai, Chinese amber, XXFarEastFont-Arial, XXFarEastFont-Franklin, etc. these familiar names

In addition to these common styles, there are some unconventional font style, of course, good fonts encountered when I will accumulate, and organized in the following, do not require more, as long as the refinement.

If there are very recommended fonts also welcome to tell me that I'm packing together.

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.