0

Somehow I can't find out if it is possible to make long text in a button wrap in KivyMD.

In classic Kivy, this is done with "text_size: self.width, None"

But in KivyMD no matter what I do the result is still one line that doesn't end anywhere.

Does anyone know how to do this?

My try: KV file:

MDScreen:
    name: 'wrap'
    md_bg_color: app.theme_cls.bg_darkest

    MDBoxLayout:
        orientation:'vertical'
        size_hint: .9, .66
        pos_hint: {'center_x': .5, 'center_y': .5 }

        MDRaisedButton:             
            text: 'asdlkasjdlaskjda asdlkasjdlaskjda aslkdjaskldjasd aslkdjaslkdjasld asldkjasldkjasd'
            size_hint_y: None
            text_size: self.width, None
            size_hint: .3, .12
            pos_hint: {'center_x': .5, 'center_y': .33 }

PY file:

from kivy.lang import Builder
from kivymd.uix.screen import MDScreen      
from kivymd.app import MDApp
from kivy.uix.screenmanager import ScreenManager
from kivymd.toast import toast

from random import randint

class MainLayout(MDScreen):
    pass

class MyApp(MDApp):
    
    def build(self):            
        
        self.screen_manager = ScreenManager()
        self.screen_manager.add_widget(Builder.load_file('wrap.kv'))

        return self.screen_manager
    
MyApp().run()

1 Answers1

0

I think that is a feature of kivymd, but you could extend MDRaisedButton to do what you want by writing your own text wrapping method. Here is a simple example, but an actual class would be considerably more complex:

class MyMDRaisedButton(MDRaisedButton):
    ignore_text_change = BooleanProperty(False)

    def on_text(self, instance, new_text):
        # this ignores the text change that this code performs
        # in order to avoid an infinite loop
        if self.ignore_text_change:
            self.ignore_text_change = False
            return
        
        # this code does the wrapping
        replacement_text = ''
        for ch in new_text:
            if ch == ' ':
                replacement_text += '\n'
            else:
                replacement_text += ch
                
        # set the ignore boolean
        self.ignore_text_change = True
        # replace the original text with the wrapped text
        self.text = replacement_text
John Anderson
  • 35,991
  • 4
  • 13
  • 36