Skip to content

Commit

Permalink
Added the feature to use icons from custom fonts.
Browse files Browse the repository at this point in the history
  • Loading branch information
HeaTTheatR committed Jan 18, 2024
1 parent 08ebd89 commit 25f242e
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 4 deletions.
12 changes: 8 additions & 4 deletions kivymd/uix/label/label.kv
Expand Up @@ -26,12 +26,16 @@

<MDIcon>
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


75 changes: 75 additions & 0 deletions kivymd/uix/label/label.py
Expand Up @@ -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 <https://fonts.google.com/icons?icon=>`_. You can find the
necessary fonts in the
`materialsymbols-python <https://github.com/T-Dynamos/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
Expand Down

0 comments on commit 25f242e

Please sign in to comment.