This article is about the actual project encountered a custom font display problems, and then use the advanced Apple Dad tools to solve the problem of the story.

1. What’s wrong with custom fonts?

Designers are constantly changing their work, and in order for them to succeed, we as programmers have to satisfy them.

So when a designer uses a magical font, I don’t say no, like this:

In order to give you a better view of this problem, I set the label with a background color, and the width and height of the label are equal to 50. Now that looks fine in our storyboard, let’s Run:

Compared with the system font label, I felt the content of DINCondensedC was too big! (Of course you don’t have to compare). WTF!!!!

2. Solution

2.1 Since it is content biased, is it related to content Mode?

Unfortunately, changing the contentMode does not have any effect on the UILabel after trying (the actual drawing contains the whitespace below).

Conclusion: Not feasible

2.2 Inherit UILabel and rewrite drawRect? Draw font with CoreText?

These two solutions should be feasible, but for this small font, is it necessary to have such a complicated process of [calculate font size] – [calculate offset by font size and label height] – [change the label using this font to XXLabel]?

Wouldn’t you cry if a designer said: Come on, let’s have rich text with dinsedc in the middle and System on the sides?

Conclusion: Not feasible

2.3 If the program can’t solve it, let’s use people to solve it

Let’s find the cute designer, buy him tea, have a nice dinner, give him a big health care, and then say to him: dude, can I change the font for this UI diagram… The cost is a little high.

Conclusion: Not feasible

2.4 Can we start from the font and modify the font by ourselves?

Since the font was a little out of order, we had to take the next big step: fix it ourselves. It is said that there is an App called Glyphs which is very powerful for making/modifying fonts! Then let’s download it and wait. Once you’ve downloaded the “PT DIN Condensed Cyrillic. TTF” font file, you can only use it for a few days at a cost. Try it and use it, but I have to change every character in this font one by one, okay? As a programmer, I can’t stand it!

Conclusion: Not feasible

3. Final solution

In the end, it comes down to Apple’s dad, everyone’s dad. Apple offers a Font modification Tool: Apple Font Tool Suite. Let’s use this tool to solve this tricky problem.

3.1 Downloading the Tool

Enter here, scroll to the bottom, you can see the [Apple Font Tool Suite], click the download below, download a version of Xcode that suits you, and install it without thinking.

3.2 Obtaining the font information file

Open the terminal and type ftxDumperfuser -t hhea-a d PT\ DIN\ Cyrillic. TTF is the command in front of ftxDumperfuser -t hhea-a d and the path to your font file in the back. Finally, press Enter and you’ll see a new [dincondensedC.hhea. XML] file next to the same folder:

3.3 Modifying the font file

Let’s open the font file and you’ll see:

<? xml version="1.0" encoding="UTF-8" standalone="no"? > <! DOCTYPE hheaTable [ <!ELEMENT hheaTable EMPTY> <!ATTLIST hheaTable versionMajor CDATA#IMPLIED
	versionMinor CDATA #IMPLIED
	ascender CDATA #IMPLIED
	descender CDATA #IMPLIED
	lineGap CDATA #IMPLIED
	advanceWidthMax CDATA #IMPLIED
	minLeftSideBearing CDATA #IMPLIED
	minRightSideBearing CDATA #IMPLIED
	xMaxExtent CDATA #IMPLIED
	caretSlopeRise CDATA #IMPLIED
	caretSlopeRun CDATA #IMPLIED
	caretOffset CDATA #IMPLIED
	metricDataFormat CDATA #IMPLIED
	numberOfHMetrics CDATA #IMPLIED>] > <! -- Data generated Sun Aug 13 18:51:10 2017 Generated by ftxdumperfuser build 347, FontToolbox.framework build 257 Font full name:'PT DIN Condensed Cyrillic'
	Font PostScript name: 'DINCondensedC'

-->


<hheaTable
	versionMajor="1"
	versionMinor="0"
	ascender="700"
	descender="209"
	lineGap="68"
	advanceWidthMax="889"
	minLeftSideBearing="270"
	minRightSideBearing="- 22"
	xMaxExtent="844"
	caretSlopeRise="1"
	caretSlopeRun="0"
	caretOffset="0"
	metricDataFormat="0"
	numberOfHMetrics="234"
	/>
Copy the code

Yes, this is an XML file that contains some common information about fonts:

  • Ascender: Distance from the baseLine to the top of the font
  • Descender: The distance from the baseline to the lowest point of the font
  • LineGap: The distance between printing lines
  • .

Each of these items can be found in apple’s: hheaTable documentation. As you can see from the documentation, a font is also a big project.

Today we’re going to fix a problem with DINCondensedC font, so let’s adjust ascender from 700 to 900 and save the file.

3.4 Injecting the modified file into the original TTF file

Open the terminal and type ftxDumperfuser -t hhea -a f PT\ DIN\ Cyrillic. TTF notice that the d after the -a has been replaced with an F and press Enter.

3.5 Replacing font files in the original project

Cut back to our project, replace the original font file, and Run:

Problem solved!

4. The Demo address

Click here to direct to the warehouse

  • CustomFontIssues for font problems project
  • CustomFontIssuesResolve is a project for resolved font issues

Welcome to taste ~