0

I'm using MDTextFieldHintText inside an MDTextField, but I can't change the MDTextFieldHintText font_size when it's nested. When I use MDTextFieldHintText outside of MDTextField, the font_size works as expected.

The reason I'm trying to set a specific dp value is that when the user changes the system font size on their phone, the hint text gets distorted or doesn't properly fit within the MDTextField bounds.

Does anyone have an idea or workaround for this issue?

from kivymd.app import MDApp
from kivy.lang import Builder
from kivymd.uix.textfield import MDTextField, MDTextFieldHintText
from kivy.metrics import dp

    KV = """
    MDScreen:
        md_bg_color: self.theme_cls.backgroundColor
    
        MDTextFieldHintText:
            text: "font_size works here..."
            theme_font_size: "Custom"
            font_size: "40dp"
    
        MDTextField:
            mode: "outlined"
            theme_font_size: "Custom"
            font_size: "50dp"
    
            MDTextFieldHintText:
                text: "...but font_size doesn't work here"
                theme_font_size: "Custom"
                font_size: "40dp"
    """
    
    class MainApp(MDApp):
        def build(self):
            self.theme_cls.theme_style = "Light"
            screen = Builder.load_string(KV)
            return screen
    
    MainApp().run()

(Kivy==2.3.1, KivyMD==development version from master branch)

Solution I found: By subclassing both MDTextField and MDTextFieldHintText, I was able to manually control the font size in dp, and adjust the vertical positioning using set_pos_hint_text.

from kivymd.app import MDApp
from kivy.lang import Builder
from kivymd.uix.textfield import MDTextField, MDTextFieldHintText
from kivy.metrics import dp

KV = """
MDScreen:
    md_bg_color: self.theme_cls.backgroundColor

    CustomMDTextField:
        mode: "outlined"
        theme_font_size: "Custom"
        font_size: "20dp"

        CustomMDTextFieldHintText:
            text: "Font size for HintText is 12dp"

"""
class CustomMDTextField(MDTextField):
    # Adjust Y-position of hint text (optional)
    def set_pos_hint_text(self, y, anim):
        super().set_pos_hint_text(y + dp(-5), anim)

class CustomMDTextFieldHintText(MDTextFieldHintText):
    # Override default font size in dp
    font_size = dp(12)

class MainApp(MDApp):
    def build(self):
        self.theme_cls.theme_style = "Light"
        screen = Builder.load_string(KV)
        return screen

MainApp().run()
2
  • 1
    With 'kivymd', you might be limited to using 'role', with sizes of 'large', 'medium', or 'small'. Commented Jun 10 at 12:57
  • I've added the solution to my original post. Commented Jun 12 at 7:35

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.