在Scrollview中动态调整标签的大小? [英] Dynamically resizing a Label within a Scrollview?

查看:111
本文介绍了在Scrollview中动态调整标签的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设您具有如下设置:

ScrollView:
   size_hint: (1, 0.5)

   Label:
      size_hint: (1, None)

最初,标签没有内容/文本.如果我理解正确,则在创建对象时,Label的高度为None.

Initially, the Label has no content/text. If I understand correctly, when the objects are created, the Label's height is None.

运行应用程序时,Label的text属性设置为多行文本,这将把Label的内容推到ScrollView的边界之外.但是,要进行滚动滚动,似乎必须动态调整Label的y尺寸/高度.

When the app is run, the Label's text property is set to multiple lines of text, which should push the content of the Label outside the boundaries of the ScrollView. However, for scrolling to happen, it seems that the Label's y dimension/height has to be dynamically resized.

根据标签的新内容动态调整标签高度的最佳方法是什么?

What is the best way to dynamically resize the height of the Label in accordance with the Label's new content such that a scrolling action can occur?

推荐答案

默认情况下,实际文本显示为大小为 not 的矩形.当文本量很大时,它会变大并且可以超出标签"范围,但不会调整标签的大小.

The actual text is displayed in a Rectangle whose size is not coupled to the Label size by default. When the amount of text is large, this grows larger and can exceed the Label bounds, but does not resize the label.

控制所显示文本的实际大小的相关属性为Label.text_size.要获得所需的行为,只需执行以下操作:

The relevant property controlling the real size of the displayed text is Label.text_size. To get the behaviour you want, you can simply do:

ScrollView:
   size_hint: (1, 0.5)

   Label:
      size_hint: (1, None)
      height: self.text_size[1]

这将绑定标签的高度以跟踪显示的文本的高度,因此应该为您提供所需的行为.

This binds the height of the label to track the height of the displayed text, and so should give you the behaviour you want.

作为旁注,更改或观看text_size通常很有用.例如,要使文本环绕在Label的边缘而不是依靠手动换行符,可以设置text_size: self.size,这意味着如果文本的宽度超过标签宽度,则文本将被自动包裹.如果要使用halign或valign来控制文本在其纹理内的位置以及标签本身在 not 中的位置,这也很重要-除非您手动将text_size设置为(再次)设置,否则这些属性将无可见效果大于文本实际占用的空间.

As a side note, it's often useful to change or watch text_size. For instance, to make the text wrap at the edges of the Label rather than relying on manual newlines, you can set text_size: self.size which means the text will be automatically wrapped if its width would exceed the label width. This is also important if working with halign or valign, which control the text position within its texture and not within the label itself - those properties will have no visible effect unless you manually set text_size to (again) something larger than the space the text actually takes up.

根据您在下面的评论,这是我的一个应用程序中的Label的示例,如果文本的长度增加(用户可以滚动),标签会垂直增长,但是当文本的宽度更大时也会自动换行比标签宽度大.看来我实际上是通过将Label放入GridLayout来完成此操作的,由于某种原因,我隐约记得它很重要.

As per your comments below, here's an example of a Label in one of my apps, which grows vertically if the length of text increases (so the user can scroll), but also automatically wraps text when its width is greater than the label width. It seems I actually did this by putting the Label in a GridLayout, which I vaguely remember was important for some reason.

GridLayout:
    cols: 1
    spacing: 10
    size_hint_y: None
    height: thetb.texture_size[1]

    Label:
        id: thetb
        text: 'whatever'
        text_size: self.width, None
        size_hint: (1, None)
        size: self.parent.width, self.texture_size[1]

您可以看到我使用text_size控制文本边界框(因此它环绕在标签边缘),但是绑定GridLayout高度以真正跟踪texture_size高度. GridLayout放置在ScrollView中,而我得到的正是我想您想要的行为.

You can see I use text_size to control the text boundingbox (so it wraps at the label edges), but bind the GridLayout height to really track the texture_size height. The GridLayout is placed in the ScrollView, and I get exactly the behaviour I think you want.

这篇关于在Scrollview中动态调整标签的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆