sequence

The use of custom fonts on Android has been a common requirement and has recently been studied in depth.

I was going to write another article about Android font modification, but there was a lot of content to write, so I decided to break it down and explain it in detail. There will be some common Fonts replacement solutions, and finally some global Fonts replacement solutions, including the latest “Fonts in XML” solution.

I look forward to your continued attention.

This is the ninth in a series of posts that have already been published.

  • Android font change overview | opening
  • Modifying fonts requires understanding all the details of Typeface
  • Simple and crude way, change the font
  • Use reflection to modify the global font
  • Use AppCompatDelegate to replace global fonts globally
  • Replace fonts globally by modifying LayoutInflater!
  • Custom properties, support for multiple font files!
  • Font in XML!

One, foreword

There are a number of fast, low-intrusion ways to replace global fonts that have been introduced before. But most of the time, we need to implement the function, must already have a ready-made implementation scheme.

This article introduces a Github, popular global replacement font open source library, almost read the document plus integration, one hour global replacement font is not a dream.

The open source replacement font library is Calligraphy:

Github.com/chrisjenx/….

Ii. How to use Calligraphy

Since you’re going to plug in an open source library to replace fonts globally, let’s take a look at what it can do.

Next, we start integrating it step by step.

2.1 Adding Gradle dependencies

Available in Calligraphy, Gradle and JAR access are supported. Gradle is used here.

2.2 Add font documents to the project

The Available files in Calligraphy can be stored in assets/. Of course, you can create a folder in it to store the typeface files.

2.3 Initialize Calligraphy

Calligraphy uses CalligraphyConfig classes to perform initializations. It needs to be called in the App entry, application.onCreate ().

The main purpose of initialization is to specify some default configurations, such as: default font, default property values.

2.4 replace the Context

The Calligraphy wraps the Activity Context, and you need to use the Context it wraps to replace the font. So you also need to rewrite the attachBaseContext BaseActivity () method, and replace it into Calligraphy provide us with the Context of the wrapper class CalligraphyContextWrapper.

2.5 the use of Calligraphy

Now that we’re done with the Calligraphy configuration, we just need to use the properties in the TextView to configure the path of our font file in assets.

2.6 Check for gaps

The Calligraphy is very easy to use and supports many different types of configuration, such as Style and Theme.

Specific use details, we still read the document to understand more convenient.

Three, the principle of Calligraphy

We use an open source library, and of course we need to understand its principles before we can use it in commercial projects. Next, we will analyze the principles of Calligraphy and see if there are any differences between them.

First look at the overall structure of Calligraphy.

As you can see, it requires very few classes, so it is a relatively lean library, and it does not rewrite the TextView, so it should replace fonts in other ways.

We will look at the source code for configuring classes that need to be called in Application, CalligraphyConfig.

CalligraphyConfig used Builder patterns to initialize itself and we could see that only configuration items were set up, no actual business logic.

CalligraphyConfig, once initialized, was stored as static variables for use elsewhere, a singleton pattern, but did not consider thread safety issues.

Given that there is no practical logic, how should important code be tracked next?

Look carefully before configuration items, need to rewrite the Activity attachBaseContext () method, here will deliver it rewrite a Context wrapper class CalligraphyContextWrapper, So we will see next CalligraphyContextWrapper source logic.

After reading the CalligraphyContextWrapper source code, you will find it the most important thing is to rewrite the getSystemService () method, when it is LAYOUT_INFLATER_SERVICE, His CalligraphyLayoutInflater class, return home.

So what exactly is LAYOUT_INFLATER_SERVICE?

Layoutinflaters are used when loading views from layout-XML.

Take a closer look at the source code for the LayoutInflater. From () method.

As you can see, when we get LayoutInflater objects, we use LAYOUT_INFLATER_SERVICE.

So CalligraphyContextWrapper. GetSystemService () method is to rewrite the purpose, is to replace the LayoutInflater object, so you can guess, set the custom font, Right there in your custom LayoutInflater.

CalligraphyLayoutInflater continue to view the source code, and ultimately change the font of logic, is in the CalligraphyContextWrappe onViewCreatedInternal () method.

It’s going to take the value that we set on our custom property and set it to our initialized TextView.

Iv. The Calligraphy summary

This completes the main points of logic tracing in Calligraphy, which are the core techniques:

  1. The Calligraphy does not need to rewrite a control like TextView.
  2. Calligraphy rewrote LayoutInflater.
  3. Calligraphy inattachBaseContext()Method, replace ContextWrapper.
  4. Also via custom ContextWrappergetSystemService()Method, will be re-made CalligraphyLayoutInflater LayoutInflater replaced with pool.
  5. In CalligraphyLayoutInflater, intercepting the TextView we need and its subclasses, for they replaced the font we set the font.

Of course, in fact, the open source library can spread more widely, it also does more details, but we generally analyze open source libraries, only need to care about the mainline logic.

Generally speaking, there are no major problems in Calligraphy and it is safe to use them. However, there may be conflicts if you use third-party libraries that also rely on the same principles, so it has to be analyzed on a case-by-case basis.

Today in the background of chengxiang Ink shadow public account, reply “growth”. I will send you some of my collated learning materials, including: Android decompilation, algorithm. Web project source code.

Recommended reading:

  • Glide used this way, more savings!!
  • If you don’t think about this before jumping ship, it’s a waste of time!!
  • Those years will pay you back
  • How do you do screen adaptation when you only have one device
  • Android source code, online reading more convenient!!

Like it or share it