如何在QML中设置文本组件文本内容中的文本不超过其宽度和高度

How to set text inside Text componenttext content not to exceed its width and height in QML

我有一个 Rectangle 并且在它上面有一个 Text 项目。后者中包含的文本确实超出了其边界。

这是我的代码:

Rectangle{
    width: 100
    height: 100
    anchors.centerIn: parent
    color: "lightblue"

    Text{
        width: parent.width
        height: parent.height
        text: "hi hello how are you. good how do you dosdfskdjgbksajgsjdfsjadfsad \n"+
              "sdfvbjsdkfjsbdvfsd sjkdbfvskdbfvskdbvs,dv jskbdvksdbvasd \n"+
              "sajbfwkbedcv klanfuoigbefhbsdaf csa djsdagfbksdjbfvsadkjvABEGFW\n"+
              "JGBGFWJAHGRJWEKHYJGWKGBFWHE  gofdgfdfgdf"
        wrapMode: Text.WordWrap
        fontSizeMode: Text.Fit
    }

}

documented:

Text.WordWrap - wrapping is done on word boundaries only. If a word is too long, contentWidth will exceed a set width.

这就是您的文本发生的情况。您可能想使用 Text.Wrap:

Text.Wrap - if possible, wrapping occurs at a word boundary; otherwise it will occur at the appropriate point on the line, even in the middle of a word.

结合一些省略,文本将完全适合 Rectangle:

elide: Text.ElideRight