Search code examples
kivyfont-sizetextinput

Kivy TextInput how to change hint_text font size


Is there a way to change a TextInput hint_text font size in Kivy? I could not find any documentation on using something as hint_text_size.

TextInput:
    id: text_input_unique
    hint_text: 'example: Stand25'
    hint_text_size: 16
    multiline: False
    size_hint_y: None
    height: 50
    font_size: 32
    halign: 'center'
    cursor_color: (0,0,0,1)

Solution

  • The TextInput uses the same font properties for hint_text as it does for the main text (except for color). Here is an extension of TextInput that honors a hint_font_size property:

    class TextInputwHintSize(TextInput):
        hint_font_size = NumericProperty(sp(15))
    
        def __init__(self, **kwargs):
            self.regular_font_size = sp(15)
            self.ignore_font_size_change = False
            super(TextInputwHintSize, self).__init__(**kwargs)
            Clock.schedule_once(self.set_font_size)
    
        def set_font_size(self, dt):
            self.ignore_font_size_change = True
            if self.text == '':
                self.font_size = self.hint_font_size
    
        def on_font_size(self, instance, size):
            if self.ignore_font_size_change:
                return
            self.regular_font_size = size
    
    
        def on_text(self, instance, text):
            if text == '':
                self.font_size = self.hint_font_size
            else:
                self.font_size = self.regular_font_size
    

    for example, use this like:

    TextInputwHintSize:
        id: text_input_unique
        hint_text: 'example: Stand25'
        hint_font_size: 16
        multiline: False
        size_hint_y: None
        height: 50
        font_size: 32
        halign: 'center'
        cursor_color: (0,0,0,1)