Truncating Long kivy.uix.label.text

1,278 views
Skip to first unread message

Ajoburg

unread,
Jul 14, 2013, 7:35:24 PM7/14/13
to kivy-...@googlegroups.com
Is there an option to truncate long text in labels instead of having it wrap? I'm doing this manually in my app now, but would be much better if the Label widget could deal with this directly.

Also, is it possible to force a label to be single line (and/or specify the specific number of lines it can have...similar to in iOS)?

Kovak

unread,
Jul 14, 2013, 9:59:29 PM7/14/13
to kivy-...@googlegroups.com
I think you are looking for the shorten property in the case of the first question.
http://kivy.org/docs/api-kivy.uix.label.html

Andy Jagoe

unread,
Jul 14, 2013, 10:41:40 PM7/14/13
to kivy-...@googlegroups.com
Thanks Kovak. Do you or anyone else know if there is any  further documentation on this? As the docs say, I am getting strange behavior while trying to set an appropriate text size. I'm not sure about text_size using a pixel x, y value...this seems to make it very difficult to select the right size to so text is not hidden or wrapped in an unusual manner...especially when changing fonts. I really like the way iOS specifies lines and not pixels for elements like these...maybe a future feature?


--
You received this message because you are subscribed to a topic in the Google Groups "Kivy users support" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/kivy-users/IrxH2sr8qmM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to kivy-users+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Adam Pospíšil

unread,
Jul 15, 2013, 8:05:33 AM7/15/13
to kivy-...@googlegroups.com, an...@jagoe.com
I'm having similar issue, is there a way to set relative text_size, some sort of 'text_size_hint'?

Akshay Arora

unread,
Jul 15, 2013, 9:49:30 AM7/15/13
to kivy-...@googlegroups.com
Can you guys explain in a bit more elaborate manner what you guys are looking to do?
Maybe I am missing(can't envision) the kind of feature you guys are looking for.
A clear explanation of what you are trying would help.

Best Regards



--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.

Akshay Arora

unread,
Jul 15, 2013, 9:57:39 AM7/15/13
to kivy-...@googlegroups.com
for example one of the major use case is to allow text to flow/increase vertically.

Label:
    text_size: '300pt', None
    # texture_size is constrained in width to 300pt and unrestricted on height
    size_hint: None, None
    size: self.texture_size

(courtesy tshirtman)
So here the text just expands vertically, so does the label size.

You should also look at the text-align example and
http://stackoverflow.com/questions/15334280/kivy-button-text-alignment-issue

If you need a feature that can't be implemented using these properties, then please,
mention them in a clear and concise manner.

Best regards

Adam Pospíšil

unread,
Jul 15, 2013, 10:47:04 AM7/15/13
to kivy-...@googlegroups.com
suppose i have this:
class MyApp(App):
    def build(self):
        rw = BoxLayout()
        for i in range(5):
            rw.add_widget(Button(text = 'very very very very very very long text'))
        return rw
MyApp().run()

My goal is to shorten text in each button so it won't spill to it's neighbours. The size of the buttons is handled by the layout and unknown to me, so I can't set the text_siz
e properly...

Andy Jagoe

unread,
Jul 15, 2013, 11:04:53 AM7/15/13
to kivy-...@googlegroups.com
The use case I have is dealing with metadata labels and never wanting text to wrap. For example, I have a label for city name. How big should I make the label? Some city names are very long, bust most aren't. Having the text wrap and spill beyond the label into neighboring elements makes the UI look buggy...so what I want to do us force the label to use only one line and to truncate text at self.width.

I have a similar (but worse) problem with the label for place names because these vary even more than city names. 

The behavior I'm describing is very similar to how iOS handles labels. You specify how many lines your label is permitted to have and then truncated at self.width (minus any padding).

In both Adam and my use cases we would set lines=1. If I remember correctly, I think android deals with this via a nowrap flag. Either one would work just fine.

Akshay Arora

unread,
Jul 15, 2013, 11:59:30 AM7/15/13
to kivy-...@googlegroups.com
suppose i have this:
class MyApp(App):
    def build(self):
        rw = BoxLayout()
        for i in range(5):
            rw.add_widget(Button(text = 'very very very very very very long text'))
        return rw
MyApp().run()

My goal is to shorten text in each button so it won't spill to it's neighbours. The size of the buttons is handled by the layout and unknown to me, so I can't set the text_siz
e properly...


You can override the button class and use it like so

    Builder.load_string('''
    <MyButton>
        text_size: self.size
        shorten: True
    '''')

    class MyButton(Button):
        pass

    class MyApp(App):

    def build(self):
        rw = BoxLayout()
        for i in range(5):
            rw.add_widget(MyButton(text = 'very very very very very very long text'))
        return rw
    MyApp().run()
 
Hope this helps.

Akshay Arora

unread,
Jul 15, 2013, 12:09:10 PM7/15/13
to kivy-...@googlegroups.com
The solution I wrote for Adam should also work for you, However this would only work for one line. I guess we could use a feature like `lines: 2`. Please open a feature request for this on gthub.com/kivy/kivy/issues

Best Regards

Andy Jagoe

unread,
Jul 16, 2013, 6:04:26 PM7/16/13
to kivy-...@googlegroups.com
Hi Akshay,

Thanks for your response. Happy to open an issue for lines and will do that now.

On the curent issue, it appears that there is still an issue that emerges with shorten when a single word extends beyond the confines of the label.size. When this happens, the text does not shorten but rather spills over beyond the label's borders and cannot be seen. If there is a very long word that doesn't fit followed by a space and more words, the shorten function will wrap to a second line even and the top of the first line and the bottom of the second line will not be visible.

Here's an example use case and also how to replicate. Imagine you have a UI that contains people's faces and you want to put a label on each face containing the person's name. If the canvas is 125 wide by 25 tall and the font is 22, the name John of course works just fine. With shorten=True, 'John Smith' also works fine as it is shortened appropriately. However, if the name is 'Michaelangelo', the shorten attribute doesn't work and the 'M' and 'o' are partically hidden from view instead of something like 'Michaelan..." all fitting inside the label which is what I had expected.

Sample .kv file for this example:

    FloatLayout:
        id: name_layout
        size_hint: 1, None
        height: 25
        canvas:
            Color:
                rgba: 0,0,0,.65
            Rectangle:
                pos: self.pos
                size: self.size
        Label:
            id: name_label
            size_hint: 1, 1
            text_size: self.size
            shorten: True
            valign: 'middle'
            halign: 'center'
            color: (1, 1, 1, 1)
            font_size: 22
            text: ''

If the height had been 50 and the text was "Michaelangelo Smith", what you would see is the text actually wrap to a second line but 'M' and 'o' still obscured and the top half of Michaelangelo hidden from view and the bottom half of "Smith" hidden from view.

Are there some other options that we could consider for how text is handled inside labels?
Reply all
Reply to author
Forward
0 new messages