LearningtoCodee
LearningtoCodee

Reputation: 83

How to use KivyMD fonts in Kivy

I have downloaded the KivyMD (Kivy Material Design) because of the basic looking natural design of Kivy.

However, I am unsure of how to use everything that comes with it?

I've tried to do the normal import of a font name from KivyMD, and the font didn't run

    Label:
        text: 'Logged in!'
        font_size: 100
        font_name: 'Material_Design_Iconic_Font'

Upvotes: 1

Views: 5155

Answers (2)

Keiko Mori
Keiko Mori

Reputation: 167

fonts style example:

KV = '''
Screen:
MDLabel:
text: "JetBrainsMono"
halign: "center"
font_style: "JetBrainsMono"
'''

from kivy.core.text import LabelBase
from kivy.lang import Builder
from kivymd.app import MDApp
from kivymd.font_definitions import theme_font_styles
class MainApp(MDApp):
def build(self):
LabelBase.register(
name="JetBrainsMono",
fn_regular="JetBrainsMono-Regular.ttf")
theme_font_styles.append('JetBrainsMono')
self.theme_cls.font_styles["JetBrainsMono"] = [
"JetBrainsMono",
16,
False,
0.15,
]
return Builder.load_string(KV)
MainApp().run()

ref

Upvotes: 0

ArtemSBulgakov
ArtemSBulgakov

Reputation: 1090

You can use KivyMD's widget MDLabel to use fonts and colors that are setted by theme (ThemeManager), or MDIcon to use Material icons.

But if you really want to use Kivy's Label widget, you can write:

#:import md_icons kivymd.icon_definitions.md_icons

BoxLayout:
    Label:
        text: f"[color=#000000][font=Roboto]Logged[/font] [font=RobotoMedium]in![/font] [font=Icons]{md_icons['login']}[/font][/color]"
        markup: True
        font_size: 100

Full code:

from kivy.lang import Builder
from kivymd.app import MDApp

root_kv = """
#:import md_icons kivymd.icon_definitions.md_icons

BoxLayout:
    Label:
        text: f"[color=#000000][font=Roboto]Logged[/font] [font=RobotoMedium]in![/font] [font=Icons]{md_icons['login']}[/font][/color]"
        markup: True
        font_size: 100
"""


class MainApp(MDApp):

    def build(self):
        self.root = Builder.load_string(root_kv)


if __name__ == "__main__":
    MainApp().run()

Upvotes: 3

Related Questions