After reading this, iOS fonts won’t be that difficult!

After reading this, iOS fonts won’t be that difficult!

[[145301]]

The motivation for writing this article is mainly to update my knowledge about iOS fonts in this blog post for my own reference in the future.

1. iOS native font display

Select the font in the label and change the font from system to custom, then you can see 72 special fonts in the family. There are some very cool fonts among them, but they are all for English numbers only and not for Chinese. I wrote a program to traverse all the native styles and display them to achieve the following effect. You can clearly see the style corresponding to each font, without having to try them one by one.

There are 72 styles in total. My demo program has two display methods, simple display and detailed display. In simple display, only the first font of each family is displayed. The last picture is a detailed display interface. It is divided into groups, and each section corresponds to a family. It can be seen that Apple's native fonts still have many beautiful fonts, but they only support English.

I believe that most software and most projects now write code like this:

  1. label.font = [UIFont systemFontOfSize: 14 ];

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

  1. UIFont *font = [UIFont fontWithName:@ "Georgia" size: 14 ];

The parameter passed into Name here is familyName instead of fontName.

2. Get the family name

So how do you get the name of this family?

Method 1: Use the label graphical interface in the storyboard or xib to select a style you like, then write down the name and write it into the code.

Method 2: It’s already there, choose from the 5 pictures above.

Method 3: (Recommended) Traversal

In the UIFont class, there are these open APIs for family names and font names. Through these, you can clearly write a traversal print to view all familyName and the fontName contained in it

  1. int i = 0 ;
  2. for (NSString *fontfamilyname in [UIFont familyNames])
  3. {
  4. NSLog(@ "family:'%@'" ,fontfamilyname);
  5. for (NSString *fontName in [UIFont fontNamesForFamilyName:fontfamilyname])
  6. {
  7. NSLog(@ "\tfont:'%@'" ,fontName);
  8. }
  9. NSLog(@ "-------------%d" ,i++);
  10. }

Use the above code to traverse and print out all the names, and then copy the names from the printout to the code, which I personally feel is more scientific.

3. External font introduction project

I have personally tested that no matter whether it is a Windows font or an Android font, as long as it is in ttf format, most iOS programs support embedding.

The specific steps are also very simple:

1. Drag the ttf file into the project

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

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

4. If you don’t want to search from the IB interface, it is recommended to use the above traversal printing. You can use the loop printing quantity to directly check whether the import is successful and find the content you need.

5. Run the project to get the results you want

4. Dynamic Fonts

Dynamic Type originated from TextKit, a text rendering framework introduced in iOS7. Its main function is to allow the system to set the size. Apple has now made more and more user-friendly processing, and even has a blind mode. The display of fonts also takes into account the preferences of each person. Some people like to read large characters, and some people like to read small characters. Before the dynamic font came out, some applications also took this user experience into consideration. For example, NetEase News used to have the function of setting the preferred font size in the application. Apple has also integrated it into the entire mobile phone. The idea of ​​dynamic fonts is: set the font size in the setting, not only the system font will change, but also the font size in the application will change accordingly. The premise is that the font code in your application meets the requirements.

Most of the previous articles talked about selecting custom in the font. If you want to consider dynamic fonts, you have to select the option in Text Styles.

  1. UIFontTextStyleHeadline
  2.  
  3. UIFontTextStyleBody
  4.  
  5. UIFontTextStyleSubheadline
  6.  
  7. UIFontTextStyleFootnote
  8.  
  9. UIFontTextStyleCaption1
  10.  
  11. UIFontTextStyleCaption2

As the names of these styles suggest, I won't list the display effects of each one. Title, subtitle, body, etc. are all relatively simple. I feel that this is equivalent to the "style" in Word. Select the title and subtitle of each chapter and set it to Title 1 or Title 2, then you can use the automatic generation of the directory function in Word, and once you change the detailed settings in a style, the format of each title and subtitle will also change accordingly. Here, if you use these styles for the font in the code, then you will react after setting the size in the phone settings and applying the font.

The location for setting the font is: Settings -> Display and Brightness -> Text Size

The left picture above shows the effect after adjusting the size to the maximum. The prompt below will only be displayed when it is adjusted to the maximum. I tried the font settings in QQ, which are all dynamic fonts. WeChat and Alipay do not support dynamic size yet. After the font size is set to a large size, WeChat and Alipay do not respond, but QQ responds to the change. The right picture above is a screenshot of the display effect of the QQ client on an iPhone 6.

The code for setting dynamic fonts is as follows:

UIFont *font = [UIFont preferredFontForTextStyle:UIFontTextStyleSubheadline];

It is recommended to combine dynamic fonts with automatic layout in the project to prevent misalignment bugs after font settings are changed.

5. Font Descriptor

Font descriptor - UIFontDescriptor is also one of the core of TextKit. The general meaning is: font descriptor can temporarily save a font style that you don't know the details of for modification or assignment to others. After using the dynamic font above, you may only know the current text-Style but don't know the detailed familyName and fontName. In this case, if you want to change the font style to italic or bold, you can only use this method:

  1. // ------Get the font style of the current text  
  2. UIFontDescriptor *bodyFontDesciptor = [UIFontDescriptor preferredFontDescriptorWithTextStyle:UIFontTextStyleBody];
  3. // ------Change the style to italic  
  4. UIFontDescriptor *italicFontDescriptor = [bodyFontDesciptor fontDescriptorWithSymbolicTraits:UIFontDescriptorTraitItalic];
  5. // ------Assign a value to another label.  
  6. self.titleLabel.font = [UIFont fontWithDescriptor:italicFontDescriptor size: 0.0 ];
  7.  
  8. There are four styles to choose from:
  9.  
  10. UIFontDescriptorTraitItalic
  11.  
  12. UIFontDescriptorTraitExpanded
  13.  
  14. UIFontDescriptorTraitCondensed
  15.  
  16. UIFontDescriptorTraitBold

The font descriptor also has an API that sets the style of a label through a detailed attribute dictionary. The syntax is as follows:

  1. UIFontDescriptor *attributeFontDescriptor = [UIFontDescriptor fontDescriptorWithFontAttributes:
  2. @{UIFontDescriptorFamilyAttribute: @ "Avenir Next Condensed" ,
  3. UIFontDescriptorNameAttribute:@ "AvenirNextCondensed-Italic" ,
  4. UIFontDescriptorSizeAttribute: @40 .0,
  5. UIFontDescriptorMatrixAttribute:[NSValue valueWithCGAffineTransform:CGAffineTransformMakeRotation(M_1_PI* 1.5 )
  6. ]}];
  7. label.font = [UIFont fontWithDescriptor:attributeFontDescriptor size: 0.0 ];

The family name, font name, size, and deformation are set here respectively. The final size can be filled in with 0.0. If a value is filled in, this value will overwrite the size in the dictionary above. I feel that under normal circumstances, no one should be so stupid as to use this method to create a style. There are many more Attributes when you click on the command, most of which are not used in daily life. If you are interested, you can study them one by one. It seems that there are a total of more than a dozen.

The label created by the above code will be displayed like this:

6. Extended font styles

As mentioned above, none of the native fonts support Chinese, but there are still many people who use Chinese. There are many search results for Chinese fonts on the Internet, but most of them are not what you want, or they will not let you download them so easily. I have compiled a commonly used font style package, which roughly includes:

Familiar names such as Huawen Xingkai, Huawen Amber, Huawen Xinwei, Lishu, etc.

Pure download address: http://pan.baidu.com/s/1hqfGdpE Password: 31qs

In addition to these commonly used styles, there are also some unconventional font styles. Of course, when I encounter good fonts, I will accumulate them and organize them below. I don’t require quantity, only quality.

Pure download address: http://pan.baidu.com/s/1i38etV3 Password: hnv7

If you have any highly recommended fonts, please let me know and I will organize them together.

<<:  Exclusive interview with Zeng Jian, senior web front-end engineer at Tencent: Node.js or H5?

>>:  Apple releases OS X 10.10.5 update, mainly to fix bugs

Recommend

Short video marketing, save these strategies quickly!

In recent years, with the sharp increase in deman...

The cost of renting bandwidth for Douyin

The larger the bandwidth, the faster the access s...

10 laws of telephone marketing!

Among the many ways to find customers, telephone ...

Will QQ red envelopes be able to stop Alipay’s counterattack?

In just over 30 days, it will be the Chinese New ...

Gaopengquan_Self-media Blue Ocean Transport Project Course Video

Gao Pengquan’s self-media blue ocean transfer pro...

Why do we need to use project thinking to attract new users?

What should you do when the KPI indicator is &quo...

A comprehensive guide to APP promotion and operation knowledge: [Exchange]

What is exchange? n The significance of exchange ...

How to gain user trust? You can start from these 9 aspects

When Dangdang held the “50 off for purchases over...

How to make a WeChat travel booking app and a scenic spot booking app?

With the rise of WeChat mini-programs, new develo...

Android 13 new features and adaptation development guide

Part 01 Feature Updates 1.1 Application icons sup...