Kivy - Text



The "kivy.core.text" module in Kivy library acts as a backend layer for rendering text. However, it should be used only if the "kivy.uix.label.Label" widget is not giving satisfactory result.

This module has two important uses: one is to derive the texture from the core Label object and apply it to any widget in the application, and second is apply custom fonts to the text property of widgets like label, button or Text input (or any widget with text property)

Using Texture

The Label widget (in "kivy.uix.label" module) is often not efficient in loading in the memory. To overcome this, the approach is to have an object of core.label class, and use its texture with the conventional widget.

First import the Label class from "kivy.core.label" (to avoid confusion, name it as CoreLabel).

from kivy.core.text import Label as CoreLabel
cl=CoreLabel(text="Hi there!", font_size=50, color=(1, 0, 0, 1))

Call the refresh() method on this object to compute things and generate the texture.

cl.refresh()

Obtain the texture and the texture size.

texture = cl.texture
texture_size = list(texture.size)

This can now be used with any widget now.

Example

In the following code, we add a label widget to a vertical box layout and use the texture of a core level object with it.

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.graphics import *
from kivy.core.text import Label as CoreLabel
from kivy.uix.label import Label
from kivy.uix.widget import Widget
from kivy.core.window import Window

Window.size = (720,400)
   class CoreLabelApp(App):
      def build(self):
         cl=CoreLabel(text="Hi there!", font_size=50, color=(1, 0, 0, 1))
         cl.refresh()
         texture = cl.texture
         main=BoxLayout(orientation='vertical')
         l=Label()
         texture_size = list(texture.size)
         l.canvas.add(Rectangle(texture=texture, size=texture_size))
         main.add_widget(l)
         return main
         
CoreLabelApp().run()

Output

It will produce the following output −

Kivy Text

Custom Fonts

All the widgets that have a text property are rendered with a default set of fonts as per the settings in the config.ini file of Kivy installation.

default_font = ['Roboto', 'data/fonts/Roboto-Regular.ttf',
'data/fonts/Roboto-Italic.ttf', 'data/fonts/Roboto-Bold.ttf',
'data/fonts/Roboto-BoldItalic.ttf']

However, you may want to use a specific font in which a text on any widget, it may be a label, or a TextInput box or a button. To do that, you need to download the relevant font file (The True Type Fonts are represented by .ttf files) and place it in the application folder.

To make these fonts available for our application, they must be registered with the application. The register() method of the LabelBase class is invoked for the purpose. LabelBase is an abstract class used by the specific font renderer used by your operating system.

The register() is a static method with following parameters −

register(name, fn_regular, fn_italic=None, fn_bold=None,
fn_bolditalic=None)

where "name" is the font name with which you will refer the font in your program, and "fn_regular" parameter is the TTF file of the font.

Download the TTF files for Consolas font and Monotype Corsiva font and store them in the application folder.

Example

In the following program, we are showing the same string in three text input boxes. The first box renders the text in the default font. The second box uses Monotype Corsiva and the third box applies the Consolas font.

from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
from kivy.core.text import LabelBase
from kivy.core.window import Window

Window.size = (720,400)
class CustomFontApp(App):
   def build(self):
      main=GridLayout(cols=1)
      LabelBase.register(name='font1', fn_regular='consolas.ttf')
      LabelBase.register(name='font2', fn_regular='MonotypeCorsivaFont.ttf')
      txt='Simple Is Better Than Complicated'
      t1=TextInput(text=txt, halign='center', font_size=48)
      t2=TextInput(
         text=txt, halign='center',
         font_name='font2', font_size=48
      )
      t3=TextInput(
         text=txt, halign='center',
         font_name='font1', font_size=48
      )
      main.add_widget(t1)
      main.add_widget(t2)
      main.add_widget(t3)
      return main
   
CustomFontApp().run()

Output

It will produce the following output window −

Kivy Text Font
Advertisements