How to add custom fonts in Kivy - Python?


Kivy has the ability to use and add custom fonts to the widgets that are being used in the applications, which can help developers to add a personalized and unique touch to their applications. Adding a custom font in kivy involves two major steps which are loading the custom font in kivy, and after that applying the font to the appropriate widgets like a button or a window. This can be done by using the pre-defined methods that are available in kivy.

In this article, we will discuss the process of adding custom fonts in Kivy, which will include installation of the custom fonts on the system, applying the custom font to various widgets, and loading the custom font into kivy using the CoretText provider.

How to add custom fonts in Kivy?

Kivy is an open-source Python framework that is used for creating multi-purpose applications that can run on different operating systems including Windows, Linux, macOS, Android, and iOS.

Below are the steps that we will follow to add custom fonts in Kivy −

Installing custom fonts

The first step is to install custom fonts on the system so that you can add that to your widget. Firstly, download the font file from any of the websites that are available. The extension of the file should be .ttf (TrueType font file). Next, we have to install that font. Custom fonts can be installed on the operating system the same way we install other fonts. On Windows, we can install the font simply right-click on the .ttf file and select the “Install” option. On the Mac operating system double-click on the .tts font file and select the “Install font” option. On the Linux operating system copy the font file (.ttf file) to the appropriate directory that you want.

Using custom fonts in Kivy

The next step is how to use the custom fonts in Kivy. Once we have installed the custom font, we can now easily use this font in our application. To use custom fonts in our application we have to follow these two steps: firstly, we need to load the font into the Kivy, next step is to apply the font to the appropriate widget like a window, button, etc.

Loading custom fonts in Kivy

The next step is to load the custom fonts. To load a custom font in Kivy, we need to use the CoreText provider. The CoreText provider is the platform-specific provider that allows us to use these custom fonts on MAC Operating system and iOS.

Applying Custom Fonts to Widgets

The next step after loading the custom font in Kivy, we can now apply the font to our widgets. To apply the custom font to our widget, we need to set the font_name property of the widget to the name of the custom font.

Advanced Font settings

In addition to applying and loading these custom fonts in Kivy, we can also customize the font settings, such as the style, color, and size. To customize the font setting we need to first create a Label widget and then set its properties.

Program to add custom font to a window

import kivy
kivy.require('1.11.1') # Replace with your Kivy version

from kivy.app import App
from kivy.uix.label import Label
from kivy.core.text import LabelBase, DEFAULT_FONT

class CustomFontApp(App):
   def build(self):
      # Register the custom font with Kivy
      LabelBase.register(name='CustomFont', fn_regular='font_sample.ttf')

      # Create a label widget and set its font to the custom font
      label = Label(text='Tutorialspoint!!! \n Simply easy learning at your fingertips..... ', font_name='CustomFont', font_size='50sp')
      return label

if __name__ == '__main__':
   CustomFontApp().run()

Output

Open the command prompt and type the below code in the prompt −

python matty.py

Press the enter button after pasting the above command.

Program to add custom font to a button

import kivy
kivy.require('1.11.1') # Replace with your Kivy version

from kivy.app import App
from kivy.uix.button import Button
from kivy.core.text import LabelBase

class CustomFontButtonApp(App):
   def build(self):
      # Register the custom font with Kivy
      LabelBase.register(name='CustomFont', fn_regular='font_sample.ttf')

      # Create a button widget and set its font to the custom font
      button = Button(text='This is a button', font_name='CustomFont', font_size='30sp')
      return button

if __name__ == '__main__':
   CustomFontButtonApp().run()

Output

Open the command prompt type the below and press enter.

python matty.py

Conclusion

In conclusion, we discussed in detail adding custom fonts in Kivy, the installation of the custom fonts in Windows, Mac, and Linux, loading the custom fonts into Kivy using CoreText provider, and applying the custom font to buttons and Window. We have discussed two examples one to add a custom font to a window and the other to add a custom font to a button.

By following the steps outlined in this article, you will be able to add custom fonts to your Kivy projects and make your applications more interactive and visually appealing to the users.

Updated on: 31-May-2023

564 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements