From 25f242ed22b825deac4f02bc057be2415357e343 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=98=D0=B2=D0=B0=D0=BD=D0=BE=D0=B2=20=D0=AE=D1=80=D0=B8?= =?UTF-8?q?=D0=B9?= Date: Thu, 18 Jan 2024 16:27:41 +0300 Subject: [PATCH] Added the feature to use icons from custom fonts. --- kivymd/uix/label/label.kv | 12 ++++--- kivymd/uix/label/label.py | 75 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+), 4 deletions(-) diff --git a/kivymd/uix/label/label.kv b/kivymd/uix/label/label.kv index 7550e52fd..e6b747af3 100644 --- a/kivymd/uix/label/label.kv +++ b/kivymd/uix/label/label.kv @@ -26,12 +26,16 @@ font_style: "Icon" - text: u"{}".format(md_icons[root.icon]) if root.icon in md_icons else "blank" - source: None if root.icon in md_icons else root.icon + source: None if self.icon in md_icons else self.icon adaptive_size: True + text: + ( \ + u"{}".format(md_icons[self.icon]) \ + if self.icon in md_icons else \ + "blank" \ + ) \ + if self.font_name == "Icons" else self.icon color: self.icon_color \ if self.icon_color else \ self.theme_cls.onSurfaceVariantColor - - diff --git a/kivymd/uix/label/label.py b/kivymd/uix/label/label.py index 7912eec27..0d7adb515 100755 --- a/kivymd/uix/label/label.py +++ b/kivymd/uix/label/label.py @@ -485,6 +485,81 @@ def open_context_menu(self, instance_label: CopyLabel) -> None: .. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/md-icon-badge.png :align: center + +MDIcon with a custom font icon +------------------------------ + +You can use custom fonts to display icons. Such as for example +`Material Symbols `_. You can find the +necessary fonts in the +`materialsymbols-python `_ +repository + +.. code-block:: python + + from kivy.core.text import LabelBase + from kivy.lang import Builder + from kivy.metrics import sp + + from kivymd.app import MDApp + + KV = ''' + MDScreen: + md_bg_color: self.theme_cls.backgroundColor + + MDIcon: + icon: "music_video" + theme_font_name: "Custom" + font_name: "MaterialSymbols" + pos_hint: {"center_x": .5, "center_y": .58} + + MDButton: + pos_hint: {"center_x": .5, "center_y": .47} + + MDButtonIcon: + icon: "music_video" + theme_font_name: "Custom" + font_name: "MaterialSymbols" + + MDButtonText: + text: "Elevated" + ''' + + + class Example(MDApp): + def build(self): + self.theme_cls.theme_style = "Dark" + + LabelBase.register( + name="MaterialSymbols", + fn_regular="Material_Symbols_Outlined-20-200-1_200.ttf", + ) + + self.theme_cls.font_styles["MaterialSymbols"] = { + "large": { + "line-height": 1.64, + "font-name": "MaterialSymbols", + "font-size": sp(57), + }, + "medium": { + "line-height": 1.52, + "font-name": "MaterialSymbols", + "font-size": sp(45), + }, + "small": { + "line-height": 1.44, + "font-name": "MaterialSymbols", + "font-size": sp(36), + }, + } + + return Builder.load_string(KV) + + + Example().run() + +.. image:: https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/md-icon-castom-font.png + :align: center """ from __future__ import annotations